Python Python Basics All Together Now Branch and Loop

Kasen BjuganGreen
Kasen BjuganGreen
2,142 Points

Invalid syntax with some commands

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

File "", line 31
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

Pro Student 49,926 Points

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
    # 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....
    # 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!