Python Python Basics All Together Now Handle Exceptions



I am having trouble understanding what is happening. The first ValueError I inputted was to help when the user inputted a string instead of an integer. That worked fine. I then inputted a second ValueError in order to handle when the user inputted more tickets than remaining. That works well! The problem however is when I go back to try to break the code with a string again, the error comes out weird this time.


tickets_remaining = 100

while tickets_remaining >=1: print('There are {} ticket remaining!'.format(tickets_remaining)) user_name = input('What is your name? ') tickets_wanted = input('Hi {}, how many tickets would you like to purchase? '.format(user_name)) #Expect a ValueError to happen and handle it appropriately... remember to test it out! try: tickets_wanted = int(tickets_wanted) if tickets_wanted > tickets_remaining: raise ValueError('There are only {} tickets remaining'.format(tickets_remaining)) #raise a ValueError if the request is for more tickets than are available 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: tickets_cost = tickets_wanted * TICKET_PRICE print('Great {}! Your total will be ${}'.format(user_name,tickets_cost)) choice = input('Would you like to proceed with your purchase {}?(Y/N) '.format(user_name))

  if choice.lower() == 'y':
  #Gather credit card information and then process it

    tickets_remaining -= int(tickets_wanted)
    print('{} tickets are remaining!'.format(tickets_remaining))
    print('No problem! Thank you {}!'.format(user_name))

print('I am sorry. We are all out of tickets!')```

1 Answer

It seems you are referring to the invalid literal for int() with base 10: which is a built-in system feature, it is the systems' way of handling this error, which is when you pass in a str instead of an int.

There is the same question possed by Hydar Omar under the title Getting a weird message in my error response followed by a detailed answer by Steven Parker, you should check that out in case you need more details.

Thank you @boi I will check it out!