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

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?

Thanks!

1 Answer

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 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! :-)