Error guide · Other errors
ZeroDivisionError: division by zero
A number was divided by zero, which has no answer.
Example
total = 250 count = 0 print(total / count)
What Python says
ZeroDivisionError: division by zero
As printed by Python 3.12. Other versions may word it slightly differently.
Why it happens
Division, // and % by zero are undefined in maths, so Python stops. It often happens when an average is calculated for an empty list.
How to fix it
total = 250 count = 0 if count > 0: print(total / count) else: print("No items")
Output: No items
Tips
- Check that the divisor is not zero before dividing.
- For averages, check that the list is not empty first.