Your problem is that you are using eval()
, which will try to interpret the expression as a Python expression. Python expressions can raise any number of exceptions, but EOFError
is one of the least likely.
Catch Exception
instead:
try:
print "het resultaat is:", eval(input)
except Exception:
print "Oeps, je calculatie klopt niet."
which is the base class for all 'regular' exceptions.
You can assign the exception to a local name and print out the message with your error message:
try:
print "het resultaat is:", eval(input)
except Exception as err:
print "Oeps, je calculatie klopt niet:", err
The better approach would be to parse the expression and perhaps use the functions of the operator
module to make the calculations.
The EOFError
exception is raised by the raw_input()
function when input is closed without any data being received:
>>> raw_input('Close this prompt with CTRL-D ')
Close this prompt with CTRL-D Traceback (most recent call last):
File "<stdin>", line 1, in <module>
EOFError
Unless you pass a string containing a raw_input()
or input()
call to the eval()
function you will not encounter that exception.