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

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?

6 Answers

Ian Salmon
PLUS
Ian Salmon
Courses Plus Student 10,687 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
231,248 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
231,248 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.

zoltans
zoltans
2,547 Points

I'm just wondering, why the "invalid literal for int() with base 10:" didn't come up in Craig's code. Can anyone elaborate on that? thanks

Steven Parker
Steven Parker
231,248 Points

But it did, at about 3:30 in the video when he entered "blue" instead of a number.

zoltans
zoltans
2,547 Points

Could you lease have a look if you can find something different from Craig's here, because I still get the "invalid literal for int() with base 10:" error message, alongside the user friendly message:

Please enter your full name: Rob
How many tickets would you like to purchase, Rob? blue
Oh, no...That's not a valid value. Please, try again...
invalid literal for int() with base 10: 'blue'
There are 100 tickets remaining.

Thank you

    num_tickets = input("How many tickets would you like to purchase, {}? ".format(name))
    try:
        num_tickets = int(num_tickets)
        if num_tickets > tickets_remaining:
            raise ValueError("There are only {} tickets remaining..."
.format(tickets_remaining))
    except ValueError as err:
        print("Oh, no...That's not a valid value. Please, try again...")
        print("{}".format(err))
    else:
        amount_due = calculate_price(num_tickets)
        print("The total due is £{}.".format(amount_due))
        conf = input("Would you like to proceed? Y/N: ")    
        if conf.lower() == "y":
            #TODO: gather credit card information and process it
            print("SOLD!")
            tickets_remaining -= num_tickets
        else:
            print("Thank you for your interest {}!\nHave a nice day!".format(name))
Steven Parker
Steven Parker
231,248 Points
        print("Oh, no...That's not a valid value. Please, try again...")
        # print("{}".format(err))  <-- Craig's code does not have this line

For future issues, please always start a fresh question instead of asking a question as an "answer" to another question!

zoltans
zoltans
2,547 Points

Oh, got it! Thanks a lot, I have spent such a long time on this. Sure, I'll start a fresh question next time . Still new to this.