$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · hard · #149

What is the correct fix?

total = 0
for row in rows:
    total += row.amount
average = total / len(rows)
  1. Guard against an empty rows
  2. Initialise total to 1
  3. Use sum() over the rows
  4. Convert total to a float
An empty list makes len(rows) zero, raising ZeroDivisionError.
Play Problem Solving ▸