-1

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?

Gordon
  • 13
  • 1
  • 4

1 Answers1

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