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

Python Python Basics All Together Now Branch and Loop

SyntaxError: invalid syntax on my "else" line

I can't understand why I'm getting SyntaxError: Invalid Syntax on line 41 which is my "else" statement. I believe my code is identical to Craig's but he doesn't get a syntax error while I do? There are no spaces before "else".

I have tried to erase the line and start over but it does not work: import math

TICKET_PRICE = 10

tickets_remaining = 100

Output how many tickets are remaining using the tickets_remaining variable

print("There are {} remaining tickets.".format(tickets_remaining))

Gather the user's name and assign it to a new variable

name = input("What is your name? ")

Prompt the user by name and ask how many tickets they would like

quantity = int(input("{}, how many tickets would you like? ".format(name)))

Calculate the price (number of tickets multiplied by the price) and assign that to a variable

price = quantity * TICKET_PRICE

Output the price to the screen

print("The total cost will be ${}.".format(price))

Prompt user if they want to proceed. Y/N

should_proceed = input("Would you like to proceed? Y/N. ")

If they want to proceed

if should_proceed.lower() == "y": # print out to the screen "SOLD!" to confirm purchase # TODO: Gather credit card information and process it. print("SOLD!")
# And then decrement (reduce by) the tickets remaining by the number of tickets purcahsed

tickets_remaining = tickets_remaining - quantity

Otherwise...

else: print("No worries, come back when you are ready to purchase {}!".format(name)) # Thank them by name

Console: SyntaxError: invalid syntax
treehouse:~/workspace$ python masterticket.py
File "/home/treehouse/workspace/masterticket.py", line 42
else:
^
SyntaxError: invalid syntax
treehouse:~/workspace$ python masterticket.py
File "/home/treehouse/workspace/masterticket.py", line 42
else:
IndentationError: unexpected indent
treehouse:~/workspace$ python masterticket.py
File "/home/treehouse/workspace/masterticket.py", line 41
else:
^
SyntaxError: invalid syntax

Steven Parker
Steven Parker
229,644 Points

Python is pretty much indecipherable without formatting. Use Markdown formatting to preserve the code's appearance, or an even better way to share code is to make a snapshot of your workspace and post the link to it here.

2 Answers

Are all the lines between the if statement and the else indented the same amount?

if should_proceed.lower() == "y": # print out to the screen "SOLD!" to confirm purchase 
    # TODO: Gather credit card information and process it. 
    print("SOLD!")
    # And then decrement (reduce by) the tickets remaining by the number of tickets purchased
    tickets_remaining = tickets_remaining - quantity
    # Otherwise...
else: 
    print("No worries, come back when you are ready to purchase {}!".format(name)) # Thank them by name

When posting in the forums, to format your code with Markdown you can use three backticks (`) on the lines before and after the code block. Immediately after the first three backticks, you can put the word python to specify the language.

Hey, Thank you for the tip on how to format my code!

My tickets_remaining variable was on a different indent! Thank you so much for catching that even without seeing the code

if should_proceed.lower() == "y":
    # print out to the screen "SOLD!" to confirm purchase
    # TODO: Gather credit card information and process it.
    print("SOLD!")         
    # And then decrement (reduce by) the tickets remaining by the number of tickets purchased    
    tickets_remaining = tickets_remaining - quantity    
# Otherwise...
else:
    # Thank them by name
    print("No worries, come back when you are ready to purchase {}!".format(name))

It's now working <3