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

Check tickets for number input

Hi,

When checking for number input it directs to a ValueError while the input should be an integer.

TICKET_PRICE = 10

tickets_remaining = 100

while tickets_remaining >= 1:
    print("There are {} tickets available.".format(tickets_remaining))
    name = input("What is you're name?   ")
    tickets = input("How many tickets would you like {}?   ".format(name))
    try:
        tickets = int(tickets)
        if tickets <= tickets_remaining:
            raise ValueError("There are only {} tickets remaining.".format(tickets_remaining))
    except ValueError as err:
        print("Put in only numbers! {}. Try again".format(err))
    else:
        price = tickets * TICKET_PRICE

        print("The price for {} tickets is {} dollars.".format(tickets, price))

        answer = input("Do you want to proceed with buying {} tickets? Y/N  ".format(tickets))
        if answer.lower() == "y":
            print("SOLD!")
            tickets_remaining -= tickets
        else:
            print("Thanks for visiting {}!".format(name))
print("We are sold out!")

When I put in a number it returns: "Put in only numbers!"

Okay found the solution: tickets should be > in stead of <=

if tickets > tickets_remaining:

Second thing it should raise a general exception in stead of checking for numbers, but this is actually bad habit if you ask me. How would Graig's final code respond to a non-integer value for tickets?

2 Answers

Steven Parker
Steven Parker
231,275 Points

If a non-integer was input, the video code would respond to a ValueError raised by the system when the conversion failed. It would print out something like: "Oh, no, we ran into an issue! Invalid literal for int() with base 10".

Give it a try and see!

Exactly my point. You would want a user-friendly output here like there was in the exercise before.

Steven Parker
Steven Parker
231,275 Points

As you say, creating custom messages had already been demonstrated in another exercise. But it would be simple enough to replace the standard message with a custom one here:

except ValueError as err:
    if ("invalid literal" in str(err)):
        # use custom message for bad characters
        print("You must enter a number, not letters, here!")
    else:
        # include the error text in the output
        print("Oh no, we ran into an issue.  {}  Please try again.".format(err))

Very helpful! This clears a lot. So you take the output of the error and customise based on that.