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 Branch and Loop

Gunter Ostendorp
Gunter Ostendorp
1,740 Points

Trying to prevent the rest of the code running when the # of tickets goes to 0. Haven't had success yet, heres my code.

TICKET_PRICE = 10 tickets_remaining = 100 while tickets_remaining >=1: name = input("Hello, what is your first and last name? ") ticket_amount = int(input("{}, How many tickets would you like? ".format(name))) total_price = (ticket_amount * TICKET_PRICE) print("Hello, {}".format(name)) print("Your total ticket price is, ${} dollars".format(total_price))

proceed = input("Would you like to proceed with the purchase, {}? \nY/N?".format(name))
proceed = proceed.lower()

while proceed != "y" and proceed != "n":
    proceed = input("Sorry, please state Y or N ")

if proceed == ("n"):
    print("Thank you, have a nice day, {}".format(name)) 

elif proceed == ("y"):
    tickets_remaining = (tickets_remaining - ticket_amount)
    print("Thank you for your purchase, {}!".format(name))
    print("There are {} tickets remaining".format(tickets_remaining))

print("sorry, tickets are all sold out")

2 Answers

Steven Parker
Steven Parker
218,639 Points

When posting code, use Markdown formatting to preserve the code's appearance. Otherwise, it's not possible to determine how the program loops will flow.

But as shown in the video, putting everything inside the while tickets_remaining: loop should cause it to stop when there are no tickets. If you need more explicit help, please format your code (or make a snapshot of your workspace and post the link to it) and explain what input you are providing when it runs.

inside your elif you want to change the proceed variable your while loop wont loop because once your user types "y" or "n" its not going to change unless you reset it.

So inside you elif statement on the last line after your have printed out how many tickets are remaining you want change the proceed variable back to false