What is the correct fix?
total = 0
for row in rows:
total += row.amount
average = total / len(rows)
- Guard against an empty
rows - Initialise
totalto1 - Use
sum()over the rows - Convert
totalto a float
An empty list makes len(rows) zero, raising ZeroDivisionError.
Play Problem Solving ▸