Python Python Basics All Together Now Handle Exceptions

Throws syntax error after try

I added try: number_of_tickets = int(number_of_tickets) except ValueError: print("Sorry {} thats not a valid number, please try a number this time".format(user_name))

Now I get an error Traceback (most recent call last):
File "masterticket.py", line 8, in <module>
number_of_tickets = int(number_of_tickets)
ValueError: invalid literal for int() with base 10: 'fred'

Here is all the code in the file

TICKET_PRICE = 10 tickets_remaining = 100

while tickets_remaining >= 1:
print("There are {} tickets remaining in this moment".format(tickets_remaining)) user_name = input("What's your name? ") number_of_tickets = input("Hello, {}, how many tickets would you like? ".format(user_name)) number_of_tickets = int(number_of_tickets) try: number_of_tickets = int(number_of_tickets) except ValueError: print("Sorry {} thats not a valid number, please try a number this time".format(user_name)) else:

    total_price = number_of_tickets * TICKET_PRICE
    print("The total due is ${}  ".format(total_price))
    confirmation = input("Do you want to proceed? (Y/N)  ")
    if confirmation.lower() == 'y':
        print("Sold!")
        tickets_remaining -= number_of_tickets
    else:
        print("Ups! You canceled the purchase process. Thank you anyways, {}. See you soon!!".format(user_name))

print("Sold out! Sorry, there are no tickets available ")

Please use Markdown as described in the Markdown Cheatsheet below the answer box :arrow_down:

2 Answers

KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 50,014 Points

You have number_of_tickets = int(number_of_tickets) before and after try. The one before will not be handled by ValueError. Delete the one before.

KRIS NIKOLAISEN Thanks I cant believe I overlooked that thanks so much!