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

Invalid syntax with some commands

https://w.trhou.se/6y0d9grfhe

When I tried to run the masterticket program, it gives me a syntax error that looks like this:

File "masterticket.py", line 31
else:
^
SyntaxError: invalid syntax

I don't know whats causing this, my code is almost exactly the same as the video, (I did it on my own the altered to what was in the video so it wouldn't break, but obviously it did) Any help would be appreciated, I don't know whats wrong.

2 Answers

Check line 29 for a misspelling of tickets_remaining.

tickets_remaining -= num_tickets

That line needs to be indented as well

Hey Kasen! The reason that it is saying invalid syntax is because an else statement must directly proceed an if or elif statement, in your case you have put some code in between them:

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 the tickets remaining by the number of tickets purchased
tickets_remaning -= num_tickets # This right here is in between the if/else statement. This is not allowed.
# Otherwise....
else:
    # Thank them by name
    print("Thank you anyways, {}.".format(name))

I assume this is just a simple type error, you should indent that line 4 spaces, seeing as you only want to decreate tickets_remaining if should_proceed.lower() == "y" anyway.

Finally you have not defined the variable ticket_remaning, which will also cause an error. However just correct that to the proper tickets_remaining.

Hope this helps!