Bug hunt

The Silent Except

A bare except: (with no exception type) catches everything: typos (NameError), wrong types, even you pressing Ctrl+C to stop the program. That sounds safe, but it hides your bugs.

try:
    result = totl / 2       # typo!
except:
    result = 0              # ...silently "works"

The bug report

The Guild's average_fuel(readings) should:

  • return the average of a list of numbers,
  • return 0.0 for an empty list (dividing by zero),
  • let any other problem (like a word in the list) raise its error.

But average_fuel([4, 6]) returns 0.0 instead of 5.0, and it never shows an error message. Something is hiding the real problem.

Assignment

  1. Make the except catch only ZeroDivisionError.
  2. Run the code. The hidden bug will finally show itself. Fix it.