I'm trying to create a quiz and tell the user to only enter a number, and if they don't, then give them an error message to try again. I understand you need to use something like isdigit, but don't know where it fits in?
Asked
Active
Viewed 45 times
0
-
1"but don't know where it fits in" ? maybe after you read `answer` ? just an idea... – Nir Alfasi Feb 10 '15 at 21:14
1 Answers
1
try:
answer = int(input("Enter your answer: "))
except ValueError:
print("That's not a number!")
continue
Try to convert to integer, and in case a ValueError
is thrown (it's not a valid int), print your error and restart the loop.
Also, if you give input
an argument, it prints a prompt for you. It's atypical and not that user-friendly to have them enter input all the way to the left of a blank line in the console.

Two-Bit Alchemist
- 17,966
- 6
- 47
- 82
-
This isn't a good solution as `num1` and `num2` will be reevaluated each time the user doesn't input an `int`, which probably is not the intended functionality. – MrAlexBailey Feb 10 '15 at 21:23
-
Then use an inner loop! That doesn't make this answer wrong; it just means there are two options for integrating it into the original code. Note the code in the OP doesn't even include the full loop (as the looping variable is never defined nor incremented), so it's pretty speculative to try to put together a fully integrated example. – Two-Bit Alchemist Feb 10 '15 at 21:25