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
David Tincau
Courses Plus Student 1,076 PointsWhy is my code not displaying raise ValueError
Below, why is this snippit of code not displaying when run? raise ValueError("Sorry, there are only {} tickets available".format(tickets_remaining))
Here is my entire code: TICKET_PRICE = 10
tickets_remaining = 100
while tickets_remaining >= 1: print("There are {} ticket(s) remaining".format(tickets_remaining)) user_name = input("What is your name? ") number_of_tickets = input("Hello, {}, how many tickets would you like? ".format(user_name)) try: number_of_tickets = int(number_of_tickets) if number_of_tickets > tickets_remaining: raise ValueError("Sorry, there are only {} tickets available".format(tickets_remaining)) except ValueError as err: print("That is not a valid input.".format(err)) else: user_price = number_of_tickets * TICKET_PRICE print("{}, your total price for these tickets equals ${}".format(user_name, user_price)) proceed = input("Would you like to proceed? (yes/no) ") if proceed[0].lower() == "y": #TODO: Gather credit card information and process it. print("SOLD!") tickets_remaining -= number_of_tickets else: print("Thank you {}!".format(user_name)) print("Number of tickets have reached {}, we are now sold out.".format(tickets_remaining))
1 Answer
Dorde Krstic
1,775 PointsThis is my code, I completed it yesterday. It works fine so you can see where is your mistake in the code.
SERVICE_CHARGE = 2
TICKET_PRICE = 10
tickets_remaining = 100
def calculate_price(value):
return (TICKET_PRICE * value) + SERVICE_CHARGE
while tickets_remaining >= 1:
print("There's only {} tickets left!".format(tickets_remaining))
name = input("What is your name? ")
try:
tickets = int(input("Hello {}, how many tickets would you like to buy? ".format(name)))
except ValueError:
print("You need to enter a number only")
try:
while tickets > tickets_remaining:
tickets = int(input("You can only buy {} tickets, {}. \nHow many do you want to buy? ".format(tickets_remaining, name)))
else:
price = calculate_price(tickets)
print("Price for {} tickets is ${}. Thank you {}.".format(tickets, price, name))
yes_no = input("Do you want to proceed. Y/N? ")
if yes_no.lower() == "y":
# Gather credit card information and process it.
print("SOLD! You just bought {} tickets".format(tickets))
tickets_remaining -= tickets
if tickets_remaining <= 0:
print("Tickets are sold out.")
exit()
else:
print("Tickets left: {}".format(tickets_remaining))
else:
print("Thank you anyways, {}.".format(name))
except NameError:
print("")