Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Jack Cummins
17,417 PointsI made a little program by myself! Would you please give me suggestons to make it as good as it can be!
My code is:
import sys
import random
record = []
def traffic_light():
traffic_light = input("Do you want the light to be yellow, green, or red? ").lower()
if traffic_light == 'red':
rand = random.randint(1, 10)
if rand < 3:
print("You went past the red light. $150 fine!")
record.append("Passing a red light")
else:
print("You stopped at the red light")
elif traffic_light == 'yellow':
rand2 = random.randint(1, 10)
if rand2 < 3:
print("You tried to go past the yellow light, but it turned red before you passed it. $150 fine for passing a red light!")
record.append("Passing a red light")
else:
print("You stopped at the yellow light.")
elif traffic_light == 'green':
rand3 = random.randint(1, 10)
if rand3 < 5:
print("You were speeding! $150 fine!")
record.append("speeding")
else:
print("You were driving at the speed limit.")
else:
print("You didn't type in, 'green', 'yellow', or 'red'")
def want_quit():
want_quit = input("Type 'Q' if you want to quit the app. If you want to keep using the app hit enter or return: ").lower()
if want_quit == 'q':
sys.exit()
def you_lost_your_driver_lisence():
if len(record) == 5:
print("You lost your driver's lisence!")
sys.exit()
elif len(record) == 4:
print("If you speed or pass a red light one more time than you will lose your driver's lisence!")
elif len(record) == 3 or len(record) == 2:
print("You should probably drive more carefully.")
elif len(record) == 1:
print("You have only passed a red light or sped 1 time!")
else:
print("You have a clean record!")
def print_record():
print("Your record is:")
print(record)
while True:
traffic_light()
print_record()
you_lost_your_driver_lisence()
want_quit()
Please give me suggestons on the code so that I can make it the best that it can possibly be!
Thanks!!!!!!
Jack
Wesley Trayer
13,812 PointsA fun little program... I'm ashamed to say though, I lost my drivers license. ;)
The only thing I noticed while the program was running, was all the lines printed one right after the other. It would be a little easer on the eyes if there was a blank line before and after I hit "enter" to keep playing.
Jack Cummins
17,417 PointsThis was not from a course. I came up with it myself. Thank You!
4 Answers
Jack Cummins
17,417 PointsI also made another little program. Check it out. It's not as good as the first one though.
import random
import sys
def even_odd():
number = random.randint(1, 10)
if number % 2 == 0:
print("You got an even number! It was {}!".format(number))
else:
print("You got an odd number! It was {}!".format(number))
while True:
try:
times_called = int(input("How many times do you want to run the even_odd function? "))
except:
print("Sorry, you have to type a number.")
sys.exit()
if times_called > 1000:
print("You're only allowed to print the even_odd function 1000 times or fewer.")
sys.exit()
if times_called <= 0:
print("Your number has to be between 1 and 1000")
sys.exit()
while times_called > 0:
even_odd()
times_called -= 1
want_quit = input("Press enter or return to keep going. Press 'Q' to quit. ").lower()
if want_quit == "q":
sys.exit()
else:
continue
Make sure to give suggestons if you have any!
Thanks!
Jack
P.S. Wesley Trayer I will take your suggeston.
Jack Cummins
17,417 PointsThank you Wesely Trayor!
Jack Cummins
17,417 PointsThank you Brendon Butler!
Jack Cummins
17,417 PointsHere is my new code for my traffic_light project:
import sys
import random
record = []
def traffic_light():
print()
traffic_light = input("Do you want the light to be yellow, green, or red? ").lower()
print()
if traffic_light == 'red':
rand = random.randint(1, 10)
if rand < 3:
print("You went past the red light. $150 fine!")
record.append("Passing a red light")
else:
print("You stopped at the red light")
elif traffic_light == 'yellow':
rand2 = random.randint(1, 10)
if rand2 < 3:
print("You tried to go past the yellow light, but it turned red before you passed it. $150 fine for passing a red light!")
record.append("Passing a red light")
else:
print("You stopped at the yellow light.")
elif traffic_light == 'green':
rand3 = random.randint(1, 10)
if rand3 < 5:
print("You were speeding! $150 fine!")
record.append("speeding")
else:
print("You were driving at the speed limit.")
else:
print("You didn't type in, 'green', 'yellow', or 'red'")
def want_quit():
print()
want_quit = input("Type 'Q' if you want to quit the app. If you want to keep using the app hit enter or return: ").lower()
if want_quit == 'q':
sys.exit()
def you_lost_your_driver_lisence():
print()
if len(record) == 5:
print("You lost your driver's lisence!")
sys.exit()
elif len(record) == 4:
print("If you speed or pass a red light one more time than you will lose your driver's lisence!")
elif len(record) == 3 or len(record) == 2:
print("You should probably drive more carefully.")
elif len(record) == 1:
print("You have only passed a red light or sped 1 time!")
else:
print("You have a clean record!")
def print_record():
print()
print("Your record is:")
print()
print(record)
while True:
traffic_light()
print_record()
you_lost_your_driver_lisence()
want_quit()
Do you like it?
Wesley Trayer
13,812 PointsThe blank rows definitely made the traffic light "simulator" easier to use. Good job!
Even or odd works fine; code looks clean. :)
Brendon Butler
4,254 PointsBrendon Butler
4,254 PointsYour code is clean as always. I'm not sure if this is a course in Python, but if it's not, great thinking!