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 trialNicholas Fonseca
505 PointsThrows syntax error after try
I added try: number_of_tickets = int(number_of_tickets) except ValueError: print("Sorry {} thats not a valid number, please try a number this time".format(user_name))
Now I get an error
Traceback (most recent call last):
File "masterticket.py", line 8, in <module>
number_of_tickets = int(number_of_tickets)
ValueError: invalid literal for int() with base 10: 'fred'
Here is all the code in the file
TICKET_PRICE = 10 tickets_remaining = 100
while tickets_remaining >= 1:
print("There are {} tickets remaining in this moment".format(tickets_remaining))
user_name = input("What's your name? ")
number_of_tickets = input("Hello, {}, how many tickets would you like? ".format(user_name))
number_of_tickets = int(number_of_tickets)
try:
number_of_tickets = int(number_of_tickets)
except ValueError:
print("Sorry {} thats not a valid number, please try a number this time".format(user_name))
else:
total_price = number_of_tickets * TICKET_PRICE
print("The total due is ${} ".format(total_price))
confirmation = input("Do you want to proceed? (Y/N) ")
if confirmation.lower() == 'y':
print("Sold!")
tickets_remaining -= number_of_tickets
else:
print("Ups! You canceled the purchase process. Thank you anyways, {}. See you soon!!".format(user_name))
print("Sold out! Sorry, there are no tickets available ")
2 Answers
KRIS NIKOLAISEN
54,971 PointsYou have number_of_tickets = int(number_of_tickets) before and after try. The one before will not be handled by ValueError. Delete the one before.
Nicholas Fonseca
505 PointsKRIS NIKOLAISEN Thanks I cant believe I overlooked that thanks so much!
Alexander Davison
65,469 PointsAlexander Davison
65,469 PointsPlease use Markdown as described in the Markdown Cheatsheet below the answer box