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

HELP PLEASE!

I was positive that my code is the same as Craig's but the valueError isn't running correctly. Also it still doesn't understand that there are only 100 tickets left. Please, help!

TICKET_PRICE = 10

tickets_remaining = 100  


while tickets_remaining >= 1:
    print("There are {} remaining tickets available.".format(tickets_remaining))
    name = input("What is your name?  ")
    num_tickets = input("How many tickets would you like to purchase, {}?  ".format(name))
    # Expect a ValueError to happen and handle it appropriately...remember to test it out! 
    try: 
        num_tickets = int(num_tickets)
        # Raise a ValueError if the request is for more tickets than are available 
        if num_tickets > tickets_remaining: 
            raise ValueError("There are only {} tickets remaining".format(tickets_remaining))
    except ValueError as err:
        # Include the error text in the output 
        print("Oh no, we ran into an issue. {}. Please try again".format(err))
    else:
        total = num_tickets * TICKET_PRICE
        print("The total due is ${}.".format(total))
        confirmation = input("Would you like to proceed?  (Y/N)  ")
        if confirmation.lower() == "y":
            # TODO: Gather credit card information and process it.
            print("SOLD!")
            tickets_remaining -= num_tickets
        else: 
            print("Thank you anyways, {}.".format(name))
print("I'm sorry, {}. All tickets are sold out!!! :[  ".format(name))

1 Answer

Steven Parker
Steven Parker
231,268 Points

I tried it and got this:

There are 100 remaining tickets available.
What is your name? Sam
How many tickets would you like to purchase, Sam? 4
The total due is $40.
Would you like to proceed? (Y/N) y
SOLD!
There are 96 remaining tickets available.
What is your name? Joe
How many tickets would you like to purchase, Joe? two
Oh no, we ran into an issue. invalid literal for int() with base 10: 'two'. Please try again
There are 96 remaining tickets available.

That seems to be correct operation, both in counting down the tickets and handling the ValueError. Does it do something different for you?

I think it might have been a glitch. It didn't run correctly yesterday. I switched out to a different computer, and it's working properly again. That was very weird. Thank you so much for your response, Steven. I really really appreciate it!