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 trialDaniel Skyes
1,122 PointsHow do you account for all exceptions?
After finishing this part of the course, an error was still present when we fed in a string instead of an integer. (for example "blue").
My code is exactly as it is in the video but Craig didn't check the final code with a string. My console would output:
Oh no, we ran into an issue. invalid literal for int() with base 10: 'blue'. Please try again
Any help is greatly appreciated.
Cheers
2 Answers
Claudia Xie
2,125 PointsWhat I did was remove the int() from the num_of_tickets input prompt first. I then checked if the response to the input contained all numbers. If it didn't, I raised a ValueError. Otherwise, I converted the response to int().
num_of_tickets = input('Hello, {}! How many tickets would you like?\n'.format(username))
if all(char.isdigit() for char in num_of_tickets) == False:
raise ValueError('Input is not a valid number.')
else:
num_of_tickets = int(num_of_tickets)
Steven Parker
231,248 PointsYou see that message because your program is handling the exception caused by the string input. Otherwise, the program would end instead of giving you the chance to "try again".
Bryan Land
10,306 PointsBryan Land
10,306 PointsI ran into this issue too, and this solution is really good, but its beyond the scope of this beginner course. I decided to break it down for everyone...
Hope this helps! It sure helped me to understand the all function!