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 trialChandelor Simon
2,242 PointsString coercion error still problematic
When I run masterticket, if someone enters in an invalid number it’s handled, saying, "Oh no! That's not a valid entry! There are only X tickets remaining. Do try again..."
I wanted to account for when people write in a string (i.e. blue), because with it fixed how it's specified in the videos, it shows me, "Oh no! That's not a valid entry! invalid literal for int() with base 10: 'blue' Do try again...
For the record – he does handle the string issue right before handling the invalid number issue, but when I went back to check it after accounting for the invalid number error, I got the text above ("....invalid literal for int() with base 10: 'blue'...")
So I added in the code I'll link below to try and fix this (the elif section) - and was met with a message that said "line 20..elif tickets_needed != int():... SyntaxError: invalid syntax"
I thought perhaps I needed to specify 'int():' as 'int(tickets_remaining):' but was met with the same error.
Can someone explain/show what I did wrong? Here it is:
try:
tickets_needed = int(tickets_needed)
# Tell user they've selected too many tickets
if tickets_needed > tickets_remaining:
raise ValueError("There are only {} remaining.".format(tickets_remaining))
except ValueError as err:
print("Oh no! That's not a valid entry! {} Do try again... ".format(err))
# Tell the user they have to type a number
elif tickets_needed != int():
raise ValueError("You must type a number.")
except ValueError as err_2:
print("Oh no! That's not a valid entry {} Do try again... ".format(err_2))
else:
2 Answers
Steven Parker
231,236 PointsThe "elif" isn't valid because it's in a different code block from the "if". They'd have to be in the same block to work together. Also, you would not want a second "except" for the same "try".
But it sounds like you are getting the correct error message when you enter a string. Perhaps you could explain in more detail what it is you want to accomplish with the changes.
Chandelor Simon
2,242 PointsLooking at it now I see the the block error and it's so obvious haha - and what you've shown on Tuesday is a great way to write what I was aiming for.
You're so lovely Steven. Thank you for all your help with this.
Chandelor Simon
2,242 PointsChandelor Simon
2,242 PointsSteven - thank you again for your input!
Okay, now bear in mind I'm fairly noob with this, but if I'm understanding correctly: they actually are in the same block; I just added space for the sake of showing it here. My program has no spaces like this.
Oooh - can't have two excepts; suppose that makes sense. Still - the response given to the user isn't "user-friendly" if they ask for "blue" tickets (or some other string that isn't an integer), so:
What I want to accomplish with the changes:
Basics to know: this is a program meant to tell a user how many tickets are available, ask how many they want, tell them how much that will cost, and confirm the order.
I want to make two changes. The first being: if the user says they want more tickets than I have, it says, ~"Hey, we ain't got that many, bruh - try again". The second being: if the user says they want "blue" tickets, it says, ~"Hey, that's not a number - try again".
The, "if tickets_needed > tickets_remaining" and subsequent raise, except, and print takes care of problem 1 (if they ask for 10,000,000 - the program will say, "Oh no! That's not a valid entry! There are only 'x' tickets remaining. Do try again..." but if when asked how many the user wants the user says, "blue" we get that ugly, "invalid literal for int() with base 10: 'blue' " in the middle of the pretty stuff. That's what I was aiming to fix with the elif I added.
So, I want it to say, ""Oh no! That's not a valid entry! You must type a number. Do try again..." instead of, ""Oh no! That's not a valid entry! invalid literal for int() with base 10: 'blue' Do try again..."
Steven Parker
231,236 PointsSteven Parker
231,236 PointsYou're right that the space does not define the block, but indentation does. A code block ends as soon as a line is encountered that is indented less. So the first "except" line ends the block with the "if" causing that "elif" to have no companion "if" (which is a syntax error).
To 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: