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 trialDejan Delev
461 Pointsinvalid literal?
So I followed Craig's code and wanted to test it out in the end with all the possible errors, so when I used "blue" as an answer to How many tickets I would like to buy, it printed:
How many tickets would you like to buy, Johnny? blue
Oh no, we ran into an issue. invalid literal for int() with base 10: 'blue'. Please try again!
4 Answers
Pedro Cabral
33,586 PointsWhen you write blue, the code tries to convert the string "blue" to an int which is not valid and thus throws a ValueError which you catch in your except block and handle it by printing your custom message with the exception message. What was the result you were expecting?
Dejan Delev
461 PointsI am not sure to be honest. Since we handled it before dealing with "the 1000 ticket issue", I thought it would keep handling it the same way as before, but that new message confuses me. Can I have different messages for different ValueErrors?
Pedro Cabral
33,586 PointsYes, you can for example test your app to try to break it and whenever you encounter an exception you can add an except block:
try:
// code here
except ErrorA:
// code to handle error a
except ErrorB:
// code to handle error b
Dejan Delev
461 PointsThanks a lot Pedro! You are of great help! :)
amyyxt
2,227 PointsThe error in your code actually occurs in the line before your if statement, so the interpreter doesn't read that if statement before it exits the try block and goes into the exception block. The type of error is still a ValueError, so it is caught by the "except ValueError..." block. However, the error is a separate ValueError than what the if statement would have raised. Since it's not the ValueError that the if statement raises, it doesn't have the message you gave to the ValueError in the if statement. Rather, it has the default error message for ValueErrors involving invalid int coercions.
sulaiman abouabdah
5,314 PointsHey Dejan.
Check out this solution: https://teamtreehouse.com/community/when-doing-the-valueerror-as-err-it-doesnt-solve-the-how-many-ticket-do-you-want-blue-error-we-solved-earlier Using isDigit solves the problem
Dejan Delev
461 PointsDejan Delev
461 Points