+ 1
Assert related question
How can I be sure that an user input is an integer, not a letter?
5 Answers
+ 4
You can test the input (let's call it inp) if it consists only of digits.
inp.isdigit()
It returns either True or False.
+ 3
# Using try/except/else (recommended)
a = input()
try:
int(a)
except ValueError:
# your code
print('Not an integer!')
else:
# your code
print('Integer')
# Using assert
a = input()
assert a.isdigit(), 'Not a digit!'
+ 2
You should not use assert for this, because it's only a debugging tool.
Depending on how the program is run, assert statements may be ignored by the interpreter.
Bad, if your code relies on them!
+ 2
Agree with HonFu.
Use try/else statements to handle errors