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 trialHydar Omar
1,582 PointsGetting a weird message in my error response
Code
TICKET_PRICE = 10
tickets_remaining = 100
while tickets_remaining >= 1:
print("There are {} remaining".format(tickets_remaining))
gina = input("What's your name? ")
number_tickets = input(" How many tickets would you like {}? ".format(gina))
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("Oh no, i'm not intelligent! {}. ".format(err))
else:
price_of_tickets = number_tickets * tickets_remaining
print("The tickets will cost {}".format(price_of_tickets))
proceed = input(" Do you want to proceed? Enter Y/N")
if proceed.lower() == "y":
print(" SOLD")
tickets_remaining -= number_tickets
else:
print("Thank you {}".format(gina))
print("sold out")
Response from terminal
What's your name? Monty
How many tickets would you like Monty? Python
Oh no, i'm not intelligent! invalid literal for int() with base 10: 'Python'.
3 Answers
Steven Parker
231,275 PointsIt may look "weird" but that's actually the standard system error raised when you perform an "int" operation on something that is not a number! It's working correctly.
And when posting code to the forum, use Markdown formatting to preserve the appearance (including indentation, which is essential for Python).
Leslie Lewis
1,088 PointsIs there a way of translating that message into a human language for the end user?
Steven Parker
231,275 PointsYou could certainly have some code replace that message with a more "friendly" one.
Nikolai Olekhnovitch
Front End Web Development Techdegree Graduate 25,617 PointsIs there any reason why Craig's code doesn't get the built-in "base 10" error text? My code is the same as his, but I do get that default base 10 error text. Thank you!
okilamb944
2,477 Points@Nikolai, In Craig's demo he does not test inputting a string. We don't see the error in Craig's console because he is only testing the exception when users enter an integer greater than the tickets remaining. If he ran "blue" again for number of tickets, we would have seen the same error shown in this original post.
Jon Koch
1,448 PointsJon Koch
1,448 PointsSteven, you mention in another comment we can replace the message with something more friendly. I'm having the hardest time figuring where to put this. Honestly, I don't even want to admit how long I've been fighting with it. What would a person do to change that nasty bit of error to something friendly with the ValueError exception already raised?
Steven Parker
231,275 PointsSteven Parker
231,275 PointsTo get a more a "friendly" message than the built-in error text, you could check if the exception string contains the standard text and issue your own instead:
Huong Ly Vu
Front End Web Development Techdegree Student 3,469 PointsHuong Ly Vu
Front End Web Development Techdegree Student 3,469 PointsWow, thank you very much! I have been struggling with this issue for a few days. To think that I couldn't figure this beep out, I was about to question my existence. Thanks to your answer I know my code is ok .