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
Roeniel Carter
1,637 PointsCan someone tell me if this is good code? It works fine but I feel like it could have been done better. Python btw
import sys
gameActive = False
while gameActive == False:
prompt = input("Do you wanna play RPS? (Y/N?) \n")
try:
if prompt == "y":
print("Yay!")
gameActive = True
elif prompt == "n":
print("g")
sys.exit("Bye!")
else:
raise Exception
except Exception:
print("Invalid character")
else:
print("done")
1 Answer
Chris Freeman
Treehouse Moderator 68,468 PointsWhile the code is functionally correct, if you are raising then catching your own exception you could simply replace it with a print and remove the try/except statement:
if prompt == "y":
print("Yay!")
gameActive = True
elif prompt == "n":
print("g")
sys.exit("Bye!")
else:
print("Invalid character")
Also, the comparison to False in line 5 could be replace with direct use of the "truthyness":
while not gameActive:
It can also be helpful to run flake8 on your code to catch non-standard formatting:
$ python -m flake8 your_file.py
your_file.py:5:18: E712 comparison to False should be 'if cond is False:' or 'if not cond:'
your_file.py:24:18: W291 trailing whitespace
Post back if you need more help! Good luck!!