Wednesday, 18 September 2013

How can I catch a EOF error in my calculator program

How can I catch a EOF error in my calculator program

I am trying to create a calculator. Why can't I catch the EOF error? My
program crashes when I input more than one special characters. e.g. 2++
How is this problem normally solved?
Thanks in advance!
def calcfieldcheck(input):
return re.match("^[0-9\.\-\/\*\+\%]+$",input)
#uitzoeken hoe regex nesten
def calculate(input):
print "Wat moet ik berekenen?"
counter=0
while not calcfieldcheck(input):
if counter > 0:
print "Je kan alleen getallen en expressies berekenen!"
input=raw_input("> ")
else:
print
input=raw_input("> ")
counter=counter+1
print
print "je hebt het volgende ingevoerd: ",input
try:
print "het resultaat is:", eval(input)
except EOFError:
print "EOFError, je calculatie klopt niet."
input=raw_input("> ")
print
print
counter=0

No comments:

Post a Comment