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

Asher Orr
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Asher Orr
Python Development Techdegree Graduate 9,410 Points

Invalid Literal for Int() with Base 10: How to raise a ValueError when users enter a non-integer for num_tickets.

Hi everyone! When I completed this project, I noticed that the console would deliver a funky message when I entered a non-integer (like "blue") when answering how many tickets I'd like to purchase.

The error said: invalid literal for int() with base 10: ''

Here's the code I'm working with:

 try:
        num_tickets = int(input("How many tickets would you like to buy? "))
        if num_tickets > tickets_remaining:
            raise ValueError(("There are only {} tickets left").format(tickets_remaining))
        elif num_tickets != int():
            raise ValueError("Please enter only a number when purchasing tickets")
    except ValueError as err:
        print("Oh no! We ran into an issue. {}. Please try again.".format(err))

From my understanding, this code will raise the first ValueError when num_tickets > tickets_remaining. But if that's not true, the code will then check to see if num_tickets is not equal to an integer.

If that is true, then the code should raise the second ValueError "Please enter only a number when purchasing ticket.)

I've tried several iterations, but I can't seem to get my code to refer to the ValueError "Please enter only a number when purchasing tickets" when I enter a non-integer.

Can anyone help me here? What am I missing?

1 Answer

num_tickets = int(input("How many tickets would you like to buy? ")) gets input from the user, then tries to convert the input to an integer. If the conversion fails, it throws a ValueError. If you want to have a specific error message if it isn't an integer, you could place another try-except block around it, such as

try:
    try:
        num_tickets = int(input("How many tickets would you like to buy? "))
    except ValueError:
        raise ValueError("Please enter only a number when purchasing tickets")
    else:
        if num_tickets > tickets_remaining:
            # ...

elif num_tickets != int(): compares num_tickets to the result of int(). Since no argument is passed to the int function, int() will return 0. If num_tickets is not 0, the elif statement is true, so the next line will raise the ValueError.

Asher Orr
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Asher Orr
Python Development Techdegree Graduate 9,410 Points

Hey JB, thanks for your help! It worked! I really appreciate you!

Would you mind explaining why you need to nest that within an additional try block, though? I'm not understanding why that's necessary.

Without the additional try block, invalid input for num_tickets = int(input("How many tickets would you like to buy? ")) will be thrown to

except ValueError as err:
    print("Oh no! We ran into an issue. {}. Please try again.".format(err))

and err will be something like invalid literal for int() with base 10: 'blue' and not the custom error message "Please enter only a number when purchasing tickets".

To avoid using a nested try-except block, you could change the except block to

except ValueError as err:
    if "int()" in str(err):
        err = "Please enter only a number when purchasing tickets"
    print("Oh no! We ran into an issue. {}. Please try again.".format(err))

which will change err to "Please enter only a number when purchasing tickets" when the error message contains the string "int()".