PDA

View Full Version : [Python] What's wrong with this script?



dodle
December 19th, 2008, 06:04 AM
I can't seem to figure out what is wrong with this script. I bring up an exception by replacing the operator, but my exception handler doesn't work:
#!/usr/share/env python

def evalit(CMem):
try:
if CMem[1] == "+":
FVal = float(CMem[0]) + float(CMem[2])
elif CMem[1] == "-":
FVal = float(CMem[0]) - float(CMem[2])
elif CMem[1] == "*":
FVal = float(CMem[0]) * float(CMem[2])
elif CMem[1] == "/":
FVal = float(CMem[0]) / float(CMem[2])
except:
print "\nNeed a valid operation\n"
FVal = "Error"
return FVal

CMem = []

CLoop = True
while CLoop:
while len(CMem) <= 2:
CVal = raw_input("NUMBER\n:")
CMem.append(CVal)
while len(CMem) == 1:
OVal = raw_input("OPERATOR\n:")
CMem.append(OVal)
print CMem
FVal = evalit(CMem)
print "\n%s" % str(FVal)
CMem = []

mssever
December 19th, 2008, 06:43 AM
def evalit(CMem):
try:
if CMem[1] == "+":
FVal = float(CMem[0]) + float(CMem[2])
elif CMem[1] == "-":
FVal = float(CMem[0]) - float(CMem[2])
elif CMem[1] == "*":
FVal = float(CMem[0]) * float(CMem[2])
elif CMem[1] == "/":
FVal = float(CMem[0]) / float(CMem[2])
except:
print "\nNeed a valid operation\n"
FVal = "Error"
return FVal
First, catching all exceptions with a bare except is bad practice. You should catch specific exceptions (or specific hierarchies). Doing like you're doing could easily mask a bug.

In your case, though, you're not catching any exceptions because no exceptions are getting raised. No error is occurring. Change your except block to an else block (and get rid of the try) and things will work like you expect.

dodle
December 19th, 2008, 08:12 AM
Thanks, I'll keep trying to improve my coding.