I have been trying to figure out what is wrong with the below code but in vain. The following code when run throws NameError when input is not a number. The error expected is ValueError. The except field should catch any error but does not.
print ("Enter a number")
num = input()
try:
if int(num) > 10:
print ("Number is greater than 10")
elif int(num) == 10:
print ("Number equal to 10")
else:
print ("Number is less than 10")
except:
print ("Enter valid number")
output:
Enter a number
q
Traceback (most recent call last):
File "1-try-ex.py", line 2, in <module>
num = input()
File "<string>", line 1, in <module>
NameError: name 'q' is not defined
The same code when checked on http://pythontutor.com/ gives expected behavior.
Please help to understand the issue.
Thanks in advance.