Python Python Basics All Together Now Handle Exceptions

Dejan Delev
Dejan Delev
461 Points

invalid literal?

So I followed Craig's code and wanted to test it out in the end with all the possible errors, so when I used "blue" as an answer to How many tickets I would like to buy, it printed:

How many tickets would you like to buy, Johnny? blue
Oh no, we ran into an issue. invalid literal for int() with base 10: 'blue'. Please try again!

Dejan Delev
Dejan Delev
461 Points
TICKET_PRICE = 10

tickets_remaining = 100  


while tickets_remaining >= 1:
    print("There are {} tickets remaining.".format(tickets_remaining))
    users_name = input("What's your name?  ")
    number_of_tickets = (input("How many tickets would you like to buy, {}?  ".format(users_name)))
    try:
        number_of_tickets = int(number_of_tickets)
        if number_of_tickets > tickets_remaining:
            raise ValueError("Sorry, there are only {} tickets remaining".format(tickets_remaining))
    except ValueError as err:
        print("Oh no, we ran into an issue. {}. Please try again!".format(err))
    else:
        price = int(number_of_tickets) * TICKET_PRICE
        print("That would be {}$ for your tickets".format(price))
        tickets_remaining -= int(number_of_tickets)
        answer = (input("Do you want to proceed? Y/N:  "))
        if answer.lower() == "y":
            # Gather credit card information and process it
            print("SOLD!")
        else:
            print("Thank you for your time, {}! Have a nice day!".format(users_name))
print("Sorry, we are completely sold out")

4 Answers

Pedro Cabral
Pedro Cabral
19,998 Points

When you write blue, the code tries to convert the string "blue" to an int which is not valid and thus throws a ValueError which you catch in your except block and handle it by printing your custom message with the exception message. What was the result you were expecting?

Dejan Delev
Dejan Delev
461 Points

I am not sure to be honest. Since we handled it before dealing with "the 1000 ticket issue", I thought it would keep handling it the same way as before, but that new message confuses me. Can I have different messages for different ValueErrors?

Pedro Cabral
Pedro Cabral
19,998 Points

Yes, you can for example test your app to try to break it and whenever you encounter an exception you can add an except block:

try:
    // code here
except ErrorA:
    // code to handle error a
except ErrorB:
    // code to handle error b
Dejan Delev
Dejan Delev
461 Points

Thanks a lot Pedro! You are of great help! :)

The error in your code actually occurs in the line before your if statement, so the interpreter doesn't read that if statement before it exits the try block and goes into the exception block. The type of error is still a ValueError, so it is caught by the "except ValueError..." block. However, the error is a separate ValueError than what the if statement would have raised. Since it's not the ValueError that the if statement raises, it doesn't have the message you gave to the ValueError in the if statement. Rather, it has the default error message for ValueErrors involving invalid int coercions.