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

2 Answers

Josh Keenan
Josh Keenan
19,426 Points

So I refined it into this:

tickets_remaining = 100  

while tickets_remaining > 0:
  name = input("What's your name? ")
  print("There are {} tickets left.".format(tickets_remaining))
  tickets = int(input("How many tickets would you like to buy? "))
  if tickets > tickets_remaining:
    print("You can only buy up to {} tickets!".format(tickets_remaining))
    price = tickets * TICKET_PRICE
    tickets_remaining = tickets_remaining - tickets
    print("You bought {} tickets for ${}".format(tickets, price))
    print("There are {} tickets left.".format(tickets_remaining))
    if tickets_remaining <= 0:

I'll explain the issues you had, and how this works.

Your issue was using a new variable at the end to save the number of tickets left, it wouldn't pass back around to the loop when it started a new iteration, it would just save the new number of tickets to newTotalTickets, but the number used in the rest of the loop was tickets_remaining, which meant that tickets_remaining would just stay at 100. You also didn't limit how many tickets could be bought so it would accept 101.

In the version I have made, it prevents the user buying more tickets than are available with the if statement. If the purchase is valid we move to the else clause, and it calculates the new total, and if that's below 1 then ends the program.

Hope this makes sense and feel free to ask any questions!

Jeremiah Marcos
Jeremiah Marcos
3,639 Points

ooh! okay thank you very much i get it now, after the (break) that is where you put the print statement saying that the tickets are sold out. so where i was wrong is i didn't update the tickets_remaining value and didn't set a limit i know it may sounds like i'm just repeating my self, but i saw where I was wrong now

Josh Keenan
Josh Keenan
19,426 Points

I have full faith in you! You got this!