TryBugHunt

ZeroDivisionError: division by zero

ZeroDivisionError: division by zero in Python means you divided by something that was 0. Almost always a count or length that turned out to be zero for an input you did not test — usually an empty list.

PythonPattern: Logic errors

How do I fix ZeroDivisionError: division by zero?

ZeroDivisionError: division by zero means the right side of /, // or % was 0. The usual cause is an empty list, so len(nums) is 0, or a count that never increments. Decide what the result should be with no data, and check first: return 0 if not nums, or rate = passed / total if total else 0.

  1. 1. Print the divisorPrint the value you divide by just before the failing line. Find out why it is 0.
  2. 2. Test with empty inputCall the function with an empty list. sum([]) / len([]) always fails.
  3. 3. Check the counter incrementsIf you count items in a loop, make sure count += 1 is inside the branch that should count.

What does ZeroDivisionError: division by zero mean?

Python raises rather than returning infinity for /, // and %. Python 3.14 says "division by zero" for all of them; older versions use variants such as "float division by zero" and "integer division or modulo by zero", which all mean the same thing. The interesting question is never the division — it is why the divisor was zero, and whether that case was ever meant to be possible.

Why does ZeroDivisionError: division by zero happen?

1. Averaging an empty list

len([]) is 0. Every average function needs an answer for "no data".

Breaks

def average(nums):
    return sum(nums) / len(nums)

Works

def average(nums):
    if not nums:
        return 0
    return sum(nums) / len(nums)

2. A percentage of a total that can be zero

Works for every real report until the first one with no rows.

Breaks

rate = passed / total * 100

Works

rate = passed / total * 100 if total else 0.0

3. A loop counter that is never incremented

The count stays at its initial 0 because the increment is in the wrong branch.

Breaks

count = 0
for x in data:
    if x > 0:
        total += x
print(total / count)

Works

count = 0
for x in data:
    if x > 0:
        total += x
        count += 1

How to find it in your own code

Decide what the answer should be when the divisor is zero — 0, None, or an error the caller must handle — and write that case first, as a guard at the top of the function. A guard is clearer than wrapping the division in try/except.

Try it: fix this bug

easy

safe_ratio(0, 0) crashes: ZeroDivisionError: division by zero.

def safe_ratio(done, total):
    try:
        return done / total
    except ValueError:
        return 0
Open with hintsRuns in your browser. No account needed.

More challenges with this error

Each of these challenges runs into this error. Find the bug and fix it, no install and no account needed.

Still not sure why yours breaks?

Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.

Related error guides

View all logic errors →