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 trialRichard McKee
2,670 Pointswhile loop error
My Code seems to skip to the bottom. everytime I input into console it goes to the last print function please help!
''' TICKET_PRICE = 10
tickets_remaining = 100
# Run this code until there are no tickets
while tickets_remaining <=1:
#out put how many tickets are remaining using the tickets_remaining variable
print("there are {} Tickets Left".format(tickets_remaining))
#Gather the user's name and assign it to a new variable
name_of_user = input("what is your name? ")
#prompt the user by name and ask how many tickets they would like
num_tickets=input("thank you {}, and how many tickets".format(name_of_user))
num_tickets=int(num_tickets)
#calculate the price (not * by Price) and assign it to var
price= num_tickets * TICKET_PRICE
print("the total ammount due is ${}.".format(price))
#output the price to the screen
#prompt the user if they want to proceed. y/n
confirm_purchase=input("do you want to proceed with the purchase? y/n ")
if confirm_purchase.lower() =="y":
print("sold!")
tickets_remaining-= num_tickets
else:
print("thank you {}!".format(name_of_user))
print("sorry we are all SOLD OUT :(")
'''
2 Answers
Steven Parker
231,275 PointsI think you just have a typo. As Lucas pointed out, the "while" condition is false so the loop code gets skipped over.
Instead of "<=", you probably meant to write ">=" (greater or equal).
Richard McKee
2,670 PointsOH! thank you will add to the code
Cameron-Lee Rothenburg
4,415 PointsSteven is correct. The great way I remember is to imagine the < is a crocodile. The bigger number has to go to the "mouth"
So 1 < 10 or 10 > 1 Hope that helps you remember
Steven Parker
231,275 PointsSo   10 1
Now you've got me thinking about an "emoji programming language"...
Lucas Garcia
6,533 PointsLucas Garcia
6,533 PointsI believe your error is in your while condition while tickets_remaining <=1: But you have your tickets_remaining set to 100 so they will never be less than or equal to 1 unless you sell them. while tickets_remaining >=1: This will run the loop as long as the number of tickets are greater than or equal to 1.