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 trial![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Anthony Costanza](https://secure.gravatar.com/avatar/52e2fb624e5f378746d07a0d985c4a70?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Anthony 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](https://uploads.teamtreehouse.com/production/profile-photos/8935412/micro_MaiTaiPeteGravatar.jpg)
Peter Vann
36,426 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](https://secure.gravatar.com/avatar/52e2fb624e5f378746d07a0d985c4a70?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Anthony Costanza
2,123 PointsThank you so much!!!
![Peter Vann](https://uploads.teamtreehouse.com/production/profile-photos/8935412/micro_MaiTaiPeteGravatar.jpg)
Peter Vann
36,426 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!