Here is the code that I'm running, basically it just ask the user to type in numbers and then after I type in "done", calculate the average of them:
average = 0
total = 0.0
count = 0
while True:
num = raw_input()
if num == "done":
break
try:
int(num)
total = total + num
count = count + 1
except:
print "bad data"
average = total / count
print total, count, average
My problem is even if I type in an integer number, the except block still get executed (i.e. I get "bad data" as the output), could you tell me why is that?
SystemExitwhich is clearly not a good thing to catch), and not catching the exception for output is hiding basic problem details from you. You'd have been able to figure out the problem more easily if your except block was:except Exception as e: print "bad data", e