Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

8,824 Points

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!