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

Tickets sold, then told the event is sold out?

TICKET_PRICE = 10

tickets_remaining = 100

while tickets_remaining >= 1:
    print("There are {} tickets remaining.".format(tickets_remaining))
    user_name = input("What is your name?  ")
    number_of_tickets = input("how many tickets would you like, {}?  ".format(user_name))
    #Expect ValueError and handle appropriately
    try:
        number_of_tickets = int(number_of_tickets)
        #raise ValueError if the reqoest is for more tickets than are available
        if number_of_tickets > tickets_remaining:
            raise ValueError("There are only {} tickets left. Please try again.".format(tickets_remaining))
    except ValueError as err:
        # include error text in output
        print("Oh no! We ran into an issue. {}. Please try again".format(err))
    else:
        total_cost = TICKET_PRICE * number_of_tickets
        print("{}, the total for {} tickets is ${} dollars.".format(user_name, number_of_tickets, total_cost))
        buy_them = input("{}, Do you want to complete your purchase? Enter Y/N  ".format(user_name))
        if buy_them.lower() == "y":
            # TODO gather payment info and process it
            print("{} tickets SOLD to {} for {}!" .format(number_of_tickets, user_name, total_cost))
            tickets_remaining -= number_of_tickets
        else:
            print("Thank you, {}".format(user_name))
    print("I'm sorry, {}. The event has sold out. :( There are no more tickets available.".format(user_name))

I got this: treehouse:~/workspace$ python masterticket.py

There are 100 tickets remaining.
What is your name? Amy
how many tickets would you like, Amy? 3
Amy, the total for 3 tickets is $30 dollars.
Amy, Do you want to complete your purchase? Enter Y/N y
3 tickets SOLD to Amy for 30!
I'm sorry, Amy. The event has sold out. :( There are no more tickets available.
There are 97 tickets remaining.

What is your name? Amy
how many tickets would you like, Amy? 8
Amy, the total for 8 tickets is $80 dollars.
Amy, Do you want to complete your purchase? Enter Y/N n
Thank you, Amy
I'm sorry, Amy. The event has sold out. :( There are no more tickets available.
There are 97 tickets remaining.

I'm a little confused. ...

1 Answer

it looks like that print statement with the sold out message should be outside of your while, because the while is checking if the number of tickets is >= 1, so that last print statement doesn't make sense inside of the while.