Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics (2015) Letter Game App Exiting

Didier Borel
Didier Borel
2,837 Points

syntax 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

firedoor.py
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
MOD
Chris Freeman
Treehouse Moderator 68,082 Points

The syntax error is the indent of the else statement. The else should align with theif`:

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!")