Write Python. Understand every error.

Open the editor

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.

Try this code in Py-CRAFT

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

Learn the ideas behind it

Other common errors

Share this page: