2

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?

2
  • 2
    Side-notes: Bare excepts are dangerous (it can catch stuff like SystemExit which 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 Commented Jun 19, 2015 at 3:50
  • Thanks so much, but could you elaborate more on the SystemExit and Exception thing? Commented Jun 19, 2015 at 4:03

3 Answers 3

4
int(num) 

This returns an integer, but you aren't re-assigning num to this integer. You need to do

num = int(num) 

for that line to have effect.

Also note that you should just print the exception, to find out more information:

try: num_int = int(num) total = total + num_int count = count + 1 except ValueError as e: print e 

The reason we specifically catch ValueError is because it's very good practice to catch the exceptions you are expecting (and handle that particular exception's scenario), instead of catching everything blindly.

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks so much for your help, could you elaborate more on the ValueError thing?
sure, if you try and do something invalid like int('s'), then it is guaranteed to throw an exception. But different stuff in Python can throw different types of exceptions - in this case int('s') will always throw a ValueError, and so you should try to catch that specifically.
So does it mean that there are several kinds of errors in python and they all have their own names?
Yes, you can read more about the built-in ones here: docs.python.org/2/library/exceptions.html I say built-in because people define their own exceptions all the time as well.
So by using the ValueError here it's kind of like you would anticipate that an ValueError could happen and when it actually happens the message would be more clear than just "bad data"?
|
2

int(num) doesn't convert num in place, you have to assign the result. Change the line to:

num = int(num) 

and you should be golden.

Comments

1

int(num) doesn’t change num; it returns a new value. You could assign that new value back to num if you wanted to change it.

num = int(num) 

It’s also a good idea to catch specific errors and move as much out of the try block as possible to avoid hiding useful exception information.

average = 0 total = 0.0 count = 0 while True: num = raw_input() if num == "done": break try: num = int(num) except ValueError: print "bad data" continue total += num count += 1 average = total / count print total, count, average 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.