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.


I need help with the MasterTIcket (Try, Exception) I got stuck with a NameError


tickets_remaining = 100  

service_charge = 2

def calculate_price(number_of_tickets):
    return number_of_tickets * TICKET_PRICE + service_charge

while tickets_remaining >= 1:
    print("There are {} tickets remaining.".format(tickets_remaining))
    name = input("What is your name?  ")

        number_of_tickets = int(input("Hello!, {} How many tickets would you like?".format(name)))
        if number_of_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".format(err))

        total_price = calculate_price(number_of_tickets)
        print("The total price is, ${}".format(total_price))
        proceed = input("Are you sure you want to proceed with the purchase?\n Y/N?")
    if proceed.lower() == "y":
      tickets_remaining -= number_of_tickets

      print("Thank you anyway, {}!".format(name))
print("Tickets are all sold out.")

Below the try: number_of_tickets If i write a non-integer value, it gives me a NameError, how do I fix that?

Thank you!!! :)

1 Answer

The following needs to be indented:

if proceed.lower() == "y":
      tickets_remaining -= number_of_tickets

      print("Thank you anyway, {}!".format(name))

Oh wow!!! THANK YOU VERY MUCH SIR! Careless mistake haha