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 trialAnthony Costanza
2,123 PointsWhat if there are 50 tickets left and someone puts in an order to purchase 51 - this code will say it is sold out when
What if there are 50 tickets left and someone puts in an order to purchase 51 - this code will say it is sold out when that's not really true - how do you code it so it says your order exceeds the 50 tickets remaining?
3 Answers
Peter Vann
36,427 PointsHi Anthony
Here is a version of my solution (which goes beyond the challenge) with error handling:
TICKET_PRICE = 10
tickets_remaining = 100
while tickets_remaining >= 1:
if tickets_remaining > 1:
verb = "are"
remaining_s = "s"
else:
verb = "is"
remaining_s = ""
print("There {} {} ticket{} remaning (at $10.00 per ticket).".format(verb, tickets_remaining, remaining_s))
valid = False
while valid != True:
client_name = input("What is your name? ")
if (len(client_name) == 0):
print("*** INVALID SELECTION ***")
else:
try:
val = int(client_name)
except ValueError:
valid = True
else:
print("*** INVALID SELECTION ***")
valid = False
while valid != True:
num_ticket = input("{}, how many tickets do you want to purchase? ".format(client_name))
try:
val = int(num_ticket)
except ValueError:
print("*** INVALID SELECTION ***")
continue
if val <= tickets_remaining:
valid = True
else:
print("*** ERROR ***: There {} only {} ticket{} remaining. Try again.".format(verb, tickets_remaining, remaining_s))
num_ticket = int(num_ticket)
if num_ticket > 1:
num_s = "s"
else:
num_s = ""
amount_due = num_ticket * TICKET_PRICE
print("The total amout due is ${}.00 for {} ticket{}.".format(amount_due, num_ticket, num_s))
user_info = input("Do you want to procced Y/N? ").lower()
if user_info == "y":
print("SOLD!!!")
tickets_remaining -= num_ticket
else :
print("Thanks anyways, {}".format(client_name))
print("Tickets soldout! :( ")
From this thread:
https://teamtreehouse.com/community/can-someone-help-me-with-this-python-script
You can test it here:
https://www.katacoda.com/courses/python/playground
II hope that helps.
Stay safe and happy coding!
Anthony Costanza
2,123 PointsThank you so much!!!
Peter Vann
36,427 PointsYour question is good/sound/understandable.
I set value=false before I prompt the user for input. It stays false until the user provides a valid response (and otherwise gives an error message). And it keeps looping asking for a valid response indefinitely until a valid one is entered. Does that make sense?
I hope that helps.
Stay safe and happy coding!
Patryk Salwin
398 PointsPatryk Salwin
398 PointsI am a new python student. I understand most of your code. I have a problem with a part of your code where is : Valid = False . Why are you using always valid = false? Can you shortly explain it or send me a link with some info ? Thank You!
Jhonnatan Macias
Python Development Techdegree Student 1,971 PointsJhonnatan Macias
Python Development Techdegree Student 1,971 PointsThank you, very nice of you to post this and help the noobs!