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 Handle Exceptions

I can't figure out why I'm getting an invalid syntax

5 Answers

Steven Parker
Steven Parker
229,644 Points

Without using Markdown formatting, it's difficult to be sure, but it looks like there may be some indentation issues in this code. The indentation amount per level doesn't seem to be consistently implemented.

Review your indentation, make sure what ever amount you choose for a level is implemented consistently throughout the entire file, and be sure each line is indented to the correct stop for what it is indented to do.

If you still have trouble, repost the code using the formatting as explained in the video I linked to above.

Steven Parker
Steven Parker
229,644 Points

UPDATE: there are still a number of syntax/indentation issues, for example:

  • on line 19 there is an "else:" inside the "except" block, but it's not associated with any conditional
  • line 25 shows " s print("SOLD!")", but the "s" is not a Python keyword
  • between the "if" on line 23 and the "else" on 28 nothing is indented more than the "if"
  • that same "else" on 28 doesn't line up with the "if" (it's actually indented less)
  • the indenting levels appear to mostly be 4 spaces, but not always (lines 28/29 have only 2 spaces difference)

And that "```python" sequence to start a code block must be on a line by itself. But an even better way to share code is to make a snapshot of your workspace and post the link to it here. It takes very little space in the question but it shares all the code with no formatting issues.

Steven Parker
Steven Parker
229,644 Points

ANOTHER UPDATE — in the new code, these issues stand out:

  • the "print" on line 18 is missing a final closing parenthesis :point_right: )
  • the "else" on line 19 is indented an amount that doesn't match the other code
  • the assignment on line 27 is indented more than the previous statement (of the same nesting level)
  • the "else" on line 28 doesn't match the indenting of the corresponding "if" on line 23

'''python

TICKET_PRICE = 10

tickets_remaining = 100 while tickets_remaining >= 1:

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

name = input("What is your name?   ")
# Expect a ValueError to happen and handle it appropriately
num_tickets = input("How many tickets would you like, {}?    ".format(name))
try:   
    num_tickets = int(num_tickets)
    if num_tickets > tickets_remaining:
        raise ValueError("There are only {} remaining".format(tickets_remaining))
except ValueError as err:

    print("Oh no, we ran into an issue try again")
# include the error text in the output
print("Oh no we ran into an issue. {} Please try again.".format(err)  

amount_due = num_tickets * TICKET_PRICE
else:

    print("The total amount due is ${}  ".format(amount_due))
        should_proceed = input("Would you like to proceed \n (Enter Y or N)   ")
         if should_proceed.lower() == "y":
#  gather credit card information and process it
                print("SOLD!")

        tickets_remaining -= num_tickets    

     else:
        print("thank you anyway,{}!".format(name))

print("Sorry the tickets are all sold out!:(") '''

Steven Parker
Steven Parker
229,644 Points

Those 3 marks should be accents, not apostrophes. And you can edit the original question to fix the formatting, you don't need to post again as an "answer'. :see_no_evil:

``` :point_left: these, not these :point_right: '''

thank you

TICKET_PRICE = 10

tickets_remaining = 100

while tickets_remaining >= 1:

    print("There are {} tickets remaining".format(tickets_remaining))
    name = input("What is your name?   ")
    # Expect a ValueError to happen and handle it appropriately
    num_tickets = input("How many tickets would you like, {}?    ".format(name))
try:   
    num_tickets = int(num_tickets)
    if num_tickets > tickets_remaining:
        raise ValueError("There are only {} remaining".format(tickets_remaining))
except ValueError as err:
# include the error text in the output
    print("Oh no we ran into an issue. {} Please try again.".format(err)  

    else:   
        amount_due =  num_tickets * TICKET_PRICE
        print("The total amount due is ${}  ".format(amount_due))
        should_proceed = input("Would you like to proceed \n (Enter Y or N)   ")
        if should_proceed.lower() == "y":
#  gather credit card information and process it
       s print("SOLD!")

        tickets_remaining -= num_tickets    
      else:
        print("thank you anyway,{}!".format(name))
print("Sorry the tickets are all sold out!:(")
  Hi I'm still receiving an error ```python

TICKET_PRICE = 10

tickets_remaining = 100

while tickets_remaining >= 1:

print("There are {} tickets remaining".format(tickets_remaining))
name = input("What is your name?   ")
# Expect a ValueError to happen and handle it appropriately
num_tickets = input("How many tickets would you like, {}?    ".format(name))

try:
num_tickets = int(num_tickets) if num_tickets > tickets_remaining: raise ValueError("There are only {} remaining".format(tickets_remaining)) except ValueError as err:

include the error text in the output

print("Oh no we ran into an issue. {} Please try again.".format(err)  

else:   
    amount_due =  num_tickets * TICKET_PRICE
    print("The total amount due is ${}  ".format(amount_due))
    should_proceed = input("Would you like to proceed \n (Enter Y or N)   ")
    if should_proceed.lower() == "y":

gather credit card information and process it

   s print("SOLD!")

    tickets_remaining -= num_tickets    
  else:
    print("thank you anyway,{}!".format(name))

print("Sorry the tickets are all sold out!:(")

      ```python
TICKET_PRICE = 10

tickets_remaining = 100

while tickets_remaining >= 1:

    print("There are {} tickets remaining".format(tickets_remaining))
    name = input("What is your name?   ")
    # Expect a ValueError to happen and handle it appropriately
    num_tickets = input("How many tickets would you like, {}?    ".format(name))
try:   
    num_tickets = int(num_tickets)
    if num_tickets > tickets_remaining:
        raise ValueError("There are only {} remaining".format(tickets_remaining))
except ValueError as err:
# include the error text in the output
    print("Oh no we ran into an issue. {} Please try again.".format(err)  

    else:   
        amount_due =  num_tickets * TICKET_PRICE
        print("The total amount due is ${}  ".format(amount_due))
        should_proceed = input("Would you like to proceed \n (Enter Y or N)   ")
        if should_proceed.lower() == "y":
#  gather credit card information and process it
       s print("SOLD!")

        tickets_remaining -= num_tickets    
      else:
        print("thank you anyway,{}!".format(name))
print("Sorry the tickets are all sold out!:(")
  Im receiving an invalid syntax this is the exact error File "masterticket.py", line 20                                                                                                                                                                                
else:                                                                                                                                                                                                        
   ^                                                                                                                                                                                                         

SyntaxError: invalid syntax

Hi, sorry to bother you again, but I still seem to be getting the same invalid syntax as before, I have updated my question with my current code

Steven Parker
Steven Parker
229,644 Points

I added another update to my answer.