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 trialEnes artun
Courses Plus Student 2,364 PointsStuck in a loop.
Hi. My script for some reason seem to be stuck in a loop. I tried to add break to the end of it but I got break outside loop error. What can I do to fix it?
I ask for 56 tickets first. Receive 44 ticket left. During the second run I ask for 44 tickets but get 56 tickets left instead.
TICKET_PRICE = 10
tickets_remaining = 100
while tickets_remaining >= 1:
name = input('What is your name? ')
print(f'Welcome {name}')
buy_tickets = int(input(f'How many tickets do you want {name}? '))
total_cost = buy_tickets * TICKET_PRICE
print(f'Total cost is {total_cost} $')
ask = input('Would you like to proceed? Y/N ')
if ask.lower() == 'y':
print(f'Order received. {buy_tickets} ticket reserved.')
r_tickets = tickets_remaining - buy_tickets
print(f'{r_tickets} tickets left.')
else:
print(f'Thank you for considering us {name}.')
print('Sorry the tickets are all sold out.')
1 Answer
Steven Parker
231,271 PointsThe "tickets_remaining" is not being updated in this code, so every transaction will draw from the original 100.
So instead of creating a new total like this:
r_tickets = tickets_remaining - buy_tickets
print(f'{r_tickets} tickets left.')
You could update the count of remaining tickets instead:
tickets_remaining -= buy_tickets
print(f'{tickets_remaining} tickets left.')