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
Gideon De Villiers
Courses Plus Student 632 PointsDealing with multiple ValueErrors
TICKET_PRICE = 10
tickets_remaining = 100
while tickets_remaining >= 1:
print("There are {} tickets remaining. ".format(tickets_remaining))
name = input("What is your name? ")
ticket_request = input("Hi {}, how many tickets would you like to purchase? ".format(name))
#expect value error if non int.
try:
ticket_request = int(ticket_request)
if ticket_request > tickets_remaining:
raise ValueError("There are only {} tickets remaining. Nice try!.".format(tickets_remaining))
except ValueError as err:
print("Oh no we ran into an issue. {} Please try again.".format(err))
else:
price = TICKET_PRICE * int(ticket_request)
print("Okay {}, that will be ${}.".format(name, price))
proceed = input("Would you like to proceed Y/N? ")
if proceed.lower() == "y":
print("Sold! ")
# TODO: Gather credit card information.
tickets_remaining -= ticket_request
else:
print("Okay {}, thanks anyway! ".format(name))
print("Sorry, we are sold out! ")
This code works fine unless the user enters a non-integer as in input for ticket_request. It returns:
Oh no we ran into an issue. invalid literal for int() with base 10: 'si' Please try again.
I'd like to make this return more user-friendly. Any advice?
Gideon De Villiers
Courses Plus Student 632 PointsGideon De Villiers
Courses Plus Student 632 Points