Python Python Basics All Together Now Handle Exceptions

Dustin Bergsma
Dustin Bergsma
1,698 Points

String input error message

My code handles the errors possible just fine (ie, Requested tickets too many, or a string was input) I just want to make the error message cleaner when I input a string. Right now it reads: "Invalid Literal for int() with base 10: 'v'" if the request is simply too large, it generates the custom message we made.

        num_tickets = int(num_tickets)
        #raise a ValueError if the request is for more tickets than are available
        if num_tickets > tickets_remaining:
            raise ValueError("There are only {} tickets left".format(tickets_remaining))
    except ValueError as err:
        print("That is not a valid entry. {}. Please enter number of tickets you wish to purchase".format(err))

can someone help me here?


1 Answer

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 82,243 Points

When you input a string into the console... what kind of Exception does it return in the terminal? I suspect it'll be a TypeError.

Try catching a TypeError and setting a message for that in your code! :-)