I am making a code which requires the user to input a number to a question. I want to validate the data that the user types in, and if the answer is not an integer, I want to display an error message. I have looked around, and one solution seemed to be to use ' except ' but that gave a syntax error for me?
Asked
Active
Viewed 100 times
1 Answers
1
Here's how you would do it with try/except:
num = input('Enter a number')
try:
num = int(num)
except ValueError:
print('Not a number')

Alex Hall
- 34,833
- 5
- 57
- 89
-
It does not like ' except ' when I run the code it just gives me a syntax error. – Gordon Apr 09 '16 at 18:49
-
I can't help you more without seeing the code you wrote. Edit the question, include it, and let me know. – Alex Hall Apr 09 '16 at 18:51
-
I have figured it out, it was the indentation that was wrong. Thanks for your help. – Gordon Apr 10 '16 at 10:35