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 trialLiam Grogan
1,270 PointsCan someone please have a look at my loop .. ?
Hi - I am trying to create a loop so that when the customer is prompted if they would like to proceed, should they not provide a yes or no answer, it will ask them for a valid response. That part works great, however if the customer types either 'yes' or 'no' the loop still runs and it will then ask the customer again to provide a valid response.
Please help! :)
TICKET_PRICE = 10 SERVICE_CHARGE = 2 tickets_remaining = 100
def final_basket_price(quantity_request): return(quantity_request * TICKET_PRICE) + SERVICE_CHARGE
while tickets_remaining >= 1:
print("There are {} tickets remaining".format(tickets_remaining))
customer_name = input("What is your name? : ")
quantity_request = input("Hey {}. How many tickets would you like? : ".format(customer_name))
try:
quantity_request = int(quantity_request)
if quantity_request > tickets_remaining:
raise ValueError("There are only {} remaining.".format(tickets_remaining))
except ValueError:
print("Oh no - we ran into an issue. Please try again.")
else:
basket_price = final_basket_price(quantity_request)
print("The total cost of {} tickets is £{}".format(quantity_request,basket_price))
customer_proceed = input("Would you like to proceed with purchasing these tickets {}, yes or no? ".format(customer_name))
while customer_proceed.lower() != "yes" or customer_proceed.lower() != "no":
customer_proceed = input("Please answer either 'yes' or 'no' {}: ".format(customer_name))
if customer_proceed.lower() == "yes":
tickets_remaining -= quantity_request
print("Thanks {}. You have succesfully purchased {} tickets.".format(customer_name,quantity_request))
break
elif customer_proceed.lower() == "no":
print("No problem {}. You have succesfully cancelled your purchase of {} tickets".format(customer_name,quantity_request))
break
print("Sorry - all tickets have now sold out!")
4 Answers
Spacey Bread
6,256 Pointsdon't ask for no or check for an empty blank '''python if proceed != "yes"
'''
if it is not yes, it will loop back, even if it blank or if the user replies with a no
Nguyen Pham
4,856 Pointswhile should_proceed.lower() != "y" or should_proceed.lower() != "n" : should_proceed = input("Please answer either 'yes' or 'no' : ") if should_proceed.lower() == "y" or should_proceed.lower() == "n": break if should_proceed.lower() == "y": print("SOLD") tickets_remaining -= num_tickets elif should_proceed.lower() == "n": print("well...") Basically you need something to break out the while loop,
Gregory James
6,537 Pointsbased on my limited knowledge so far I can see that you forgot to close the input function in customer_proceed and you didn't format it
customer_proceed = input("Would you like to proceed with purchasing these tickets {}, yes or no?".format(quantity_request))
Ali Susanto
19,966 Points''' org code while customer_proceed.lower() != "yes" or customer_proceed.lower() != "no" ''' if user enter 'blabla', it will evaluate to true or true which eval to true if user enter 'yes', it will evaluate to false or true which eval to true if user enter 'no', it will evaluate to true or false, which eval to true.
it will be easier to understand if you do something like this
''' def is_valid_response(response): if response.lower() == "yes": return 1 elif customer_proceed.lower() == "no": return 1 else: return 0
while is_valid_response(customer_proceed) '''
Magnus Jensen
Python Web Development Techdegree Student 1,428 PointsMagnus Jensen
Python Web Development Techdegree Student 1,428 PointsFrom my point of perspective, it's because when you use 'break' statements in your if / elif loops it just breaks out into the While loop again.