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 trialNicole Groenewald
1,031 PointsRaise ValueError is not working the way I expected
Here's my code, when I enter blue as number of tickets it gives me this error "Houston we have a problem invalid literal for int() with base 10: 'blue' It only started doing this after raising the ErrorValue for too many tickets sold. How did I break it?
TICKET_PRICE = 10
tickets_remaining = 100
while tickets_remaining >=1:
print("There are {} tickets remaining.".format(tickets_remaining))
user_name = input("Hello, What's your name? ")
number_tickets = input("Welcome {}, How many tickets would you like to purchase? ".format(user_name))
try:
number_tickets = int(number_tickets)
if number_tickets > tickets_remaining:
raise ValueError("There are only {} tickets remaining.".format(tickets_remaining))
except ValueError as err:
print("Houston, we have a problem {}".format(err))
else:
if tickets_remaining >0:
cost = number_tickets * TICKET_PRICE
print("{} will cost ${}.".format(number_tickets,cost))
else:
print("I'm sorry, we are sold out of tickets")
purchase = input("Would you like to purchase these tickets? Y/N ")
if purchase.lower() == "y":
tickets_remaining -= number_tickets
print("Sold! Thanks for your purchase {}.".format(user_name))
else:
print("Thanks for stopping by, {}!".format(user_name))
print("I'm sorry, the tickets are sold out. :(")
1 Answer
Steven Parker
231,275 PointsNothing's broken. Since "blue" is not a number, trying to convert it into one with the int() function causes a ValueError. And "invalid literal for int() with base 10" is the internal error message that it generates.
What were you expecting?
Nicole Groenewald
1,031 PointsNicole Groenewald
1,031 PointsIt was more user friendly error until the raise ValueError as err line was added in the project. I guess I don't understand how these commands work I'm just copying them. I'll use some other resources to deepen my knowledge here.
It just wasn't the response I expected from the code.
I expected it just say "Houston, we have a problem. "
I think it's the "as" I don't understand that I need to dig into more.
Steven Parker
231,275 PointsSteven Parker
231,275 PointsThe variable you specify after "as" will be assigned an object when the exception occurs that will contain information about the exception, including a string describing the cause. For more details, see the Handling Exceptions section of the Python online documentation.
But take a look at this line:
The portion indicated by the comment adds that string on to the end of the message that gets printed. You could remove that to print just the beginning, but then you'd also miss seeing the custom "There are only {} tickets remaining." message when that happens instead.
Perhaps a better approach would be to just check for and replace the technical message with one of your own: