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 trialDidier Borel
2,837 Pointssyntax error
can someone help me with this simple problem. Syntax error on an else: I don't see what the problem is, but i can't be very complicated
import sys
input("Do you want to start the movie? Y/N")
if input()=='n'or'N':
sys.exit
else: print ("Enjoy the show!")
2 Answers
Chris Freeman
Treehouse Moderator 68,457 PointsThe syntax error is the indent of the else
statement. The else should align with the
if`:
import sys
input("Do you want to start the movie? Y/N")
if input()=='n'or'N':
sys.exit
else: print ("Enjoy the show!") # <-- unindented
Additionally, there are some structural issues.
The if
clause will always be True
due to the or 'N'
part which is always "truthy". Correct this by repeating the full comparison:
# first pass (still needs fix)
if input() == 'n' or input() == 'N':
The issue now is input() is run twice. (Three times if you count the original prompt). This can be fixed by using a variable to hold the input value:
# second pass (fixed)
result = input("Do you want to start the movie? Y/N")
if result == 'n' or result == 'N':
The body of the else
block should be on its own line and properly indented.
A blank line is used after the import and spaces are used around keywords (like or
), assignments (=
), and comparisons (==
) for readability.
Parens are needed in the call to sys.exit()
All together this would look like:
import sys
result = input("Do you want to start the movie? Y/N")
if result == 'n' or result == 'N':
sys.exit()
else:
print ("Enjoy the show!")
Didier Borel
2,837 Pointsthanks chris