Python Python Basics All Together Now Handle Exceptions

Héctor Gómez
Héctor Gómez
951 Points

When doing the "ValueError as err" it doesn't solve the "How many ticket do you want? Blue" error we solved earlier

How can we solve the Blue error again?

3 Answers

Ian Salmon
PRO
Ian Salmon
Pro Student 10,650 Points

I've seen this problem across the forums. You need another ValueError statement to specifically address input that isn't a number. The isdigit() method is great for this.

Add an 'if' statement directly under your 'try'. Something like (sorry for no syntax formatting).

try: if not tickets_requested.isdigit(): raise ValueError("That isn't a number. Please choose between 1 and {}.".format(tickets_remaining))

tickets_requested = int(tickets_requested)

if tickets_requested > tickets_remaining: ......

Let me know if you're still having trouble!

jesus perez
jesus perez
832 Points

great addition!

Elaine Youngman
Elaine Youngman
401 Points

Took me a bit to figure out how to tweak everything, but Ian's solution works perfectly!

TICKET_PRICE = 10

tickets_remaining = 100  

while tickets_remaining >=1:
    print("There are {} tickets available.".format(tickets_remaining))
    name = input("Hello! Welcome to the Python extravaganza. What is your name?  ") 
    try:
        number_requested = input("How many tickets would you like to buy, {}?  ".format(name))
        if not number_requested.isdigit():
            raise ValueError("Please enter ticket request as a whole number.")
        else:
            number_requested = int(number_requested)
            if number_requested > tickets_remaining:
                raise ValueError("You have requested more tickets than we have!")
    except ValueError as err: 
        print("Oops! Something has gone wrong. {} Try again.".format(err))
    else:
        total_price = number_requested * TICKET_PRICE
        print("OK {}, the total for {} tickets is ${}.".format(name, number_requested, total_price))
        confirm = input("Would you like to continue with the purchase? y/n ")
        if confirm.lower() == "y":
            #TODO: collect credit card information and charge
            print("SOLD! Enjoy the show, {}.".format(name))
            tickets_remaining -= number_requested
        else:
            print("Sorry to see you go {}! Have a nice day.".format(name))
print("This show is sold out!")
jesus perez
jesus perez
832 Points

worked better, thx.

Steven Parker
Steven Parker
159,145 Points

It should still handle that situation, the only difference is that the message will now include more information.

Johanna Gold
Johanna Gold
615 Points

Steven, does that not defeat the purpose of having friendly error messages? This example seems like a bad fix. I am having some trouble understanding what Ian is advising, Any clarification on that would be appreciated.

Steven Parker
Steven Parker
159,145 Points

I agree, this particular example seems to focus only on demonstrating the mechanism and doesn't do much to make the messages more "user-friendly". But once you understand how the basic mechanism works, you can certainly use the same technique to construct a much more friendly response in your own code.