Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics All Together Now Handle Exceptions

Windy Huynh Lu
Windy Huynh Lu
1,003 Points

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
215,372 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?

Windy Huynh Lu
Windy Huynh Lu
1,003 Points

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!