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

Alok Yadav
Alok Yadav
1,022 Points

Can someone point out my error in this code?

This is the part in the video where he asks to make an exception for the ValueError. I kind of Frankensteined this but it works well until the end, where it automatically prints "We're sorry! All the tickets have sold out!" even when that's not true.

Thanks in advance.

TICKET_PRICE = 10
tickets_remaining = 100  

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

        while True:
            ticket_number = input("Hello {}! How many tickets would you like to purchase?  ".format(user_name))
    # Expect a ValueError to happen and handle it appropriately.
            try:
                total_price = int(ticket_number) * TICKET_PRICE
                print("\nYour total will be ${}.".format(total_price))
                break

            except ValueError:
                print('\nOops! Please enter your response in numerals.\n')
                continue

        proceed = input("Would you like to confirm your sale of {} tickets for ${}? Y/N  ".format(ticket_number, total_price))

        if proceed.lower() == "y":
            print("\nAwesome! Your tickets have been sold!\n")
            tickets_remaining -= int(ticket_number)
            break
        #TODO: Gather and process CC information.
        else:
            print("\nNo problem! Let's try again.\n")
            continue

print("We're sorry! All the tickets have sold out.")

2 Answers

        proceed = input("Would you like to confirm your sale of {} tickets for ${}? Y/N  ".format(ticket_number, total_price))

        if proceed.lower() == "y":
            print("\nAwesome! Your tickets have been sold!\n")
            tickets_remaining -= int(ticket_number)
            break

When you use break here, you are breaking out of the while loop formed at while tickets_remaining >= 1:, so your program does not recheck the condition before continuing below the while loop. If you comment out or remove the break from this section, the while loop will continue to run and will check if tickets_remaining >= 1.

Alok Yadav
Alok Yadav
1,022 Points

Thanks so much for answering this!!

Tanzila Rafeeq
Tanzila Rafeeq
2,924 Points

you need to put away break, and you don't need continue

Alok Yadav
Alok Yadav
1,022 Points

Thanks for responding, I appreciate it! Follow up question: I enter continue there because I want it to loop back (is that the right term?) to the point where they enter the number of tickets. Don't I need to put a continue for that to happen?