You are catching the wrong Exception. You are catching a ValueError, but the code throws decimal.InvalidOperation
for various inputs that are not valid decimal values.
>python test.py
Enter number:10
>python test.py
Enter number:10.2
>python test.py
Enter number:asdf
Traceback (most recent call last):
File "test.py", line 6, in <module>
userInput = decimal.Decimal(userInput)
File "C:\Python27\lib\decimal.py", line 548, in __new__
"Invalid literal for Decimal: %r" % value)
File "C:\Python27\lib\decimal.py", line 3872, in _raise_error
raise error(explanation)
decimal.InvalidOperation: Invalid literal for Decimal: 'asdf'
>python test.py
Enter number:10.23.23
Traceback (most recent call last):
File "test.py", line 6, in <module>
userInput = decimal.Decimal(userInput)
File "C:\Python27\lib\decimal.py", line 548, in __new__
"Invalid literal for Decimal: %r" % value)
File "C:\Python27\lib\decimal.py", line 3872, in _raise_error
raise error(explanation)
decimal.InvalidOperation: Invalid literal for Decimal: '10.23.23'
Change your except
line to except decimal.InvalidOperation: