ZeroDivisionError in Python: Causes and Fixes

ZeroDivisionError happens when Python tries to divide a number by 0.

This is a common beginner error. It usually means the value on the right side of /, //, or % became zero before the calculation ran.

In this guide, you will learn:

  • what ZeroDivisionError means
  • the common reasons it happens
  • how to prevent it
  • how to debug the value that became zero

Quick fix

If the divisor might be 0, check it before dividing:

number = 10
value = 0

if value != 0:
    print(number / value)
else:
    print("Cannot divide by zero")

Output:

Cannot divide by zero

Use a condition before dividing when the divisor might be 0.

What ZeroDivisionError means

This error happens when Python tries to divide by 0.

It can happen with:

  • / for regular division
  • // for floor division
  • % for modulo

The number on the right side of the operator is called the divisor.

If the divisor is 0, Python stops and raises ZeroDivisionError.

Example that causes the error

Here is a simple example:

print(10 / 0)

Python raises this error:

ZeroDivisionError: division by zero

The problem is the value on the right side of /.
It is 0, so Python cannot complete the calculation.

The same problem happens with a variable:

number = 10
divisor = 0

print(number / divisor)

Common reasons this happens

ZeroDivisionError often appears because a value became 0 earlier in the program.

Common causes include:

  • A variable unexpectedly contains 0
  • User input is converted to a number and becomes 0
  • A calculation returns 0 and that result is reused as a divisor
  • A loop or function changes a value before division

Examples of common causes:

  • Dividing by a variable that has the value 0
  • Using int(input(...)) and the user enters 0
  • Using the result of len(...) - len(...) or another calculation that becomes 0
  • A function returns 0 and that result is used as the divisor
  • Using // or % with 0 as the second value

How to fix it

There are a few good ways to fix this error.

  • Check that the divisor is not 0 before dividing
  • Use if statements to handle the zero case
  • Validate user input before using it in math
  • Add print() statements to inspect the divisor value
  • Use try-except only when you need error-handling logic

In most beginner programs, the best fix is to prevent the division from happening when the divisor is 0.

If you are still learning how exceptions work, see Python errors and exceptions explained.

Fix method 1: check before dividing

This is usually the best option when zero is expected sometimes.

It keeps the code clear and easy to read. It also lets you show a custom message or choose another result.

total = 50
count = 0

if count != 0:
    average = total / count
    print(average)
else:
    print("Cannot calculate average because count is 0")

This approach is useful because:

  • it prevents the error before it happens
  • it makes your logic easy to understand
  • it lets you decide what should happen when zero is not allowed

You can also return a different value:

total = 50
count = 0

if count != 0:
    average = total / count
else:
    average = 0

print(average)

Fix method 2: handle the error with try-except

This method is useful when the divisor comes from uncertain input or another function.

You can catch the error and respond safely:

number = 10
divisor = 0

try:
    print(number / divisor)
except ZeroDivisionError:
    print("Cannot divide by zero")

Output:

Cannot divide by zero

Use this when you need error-handling behavior.

Do not use it to hide logic mistakes. If your program should never use 0 as a divisor, it is usually better to find out why that happened and fix the real cause.

If you want more practice with this pattern, read how to use try-except blocks in Python or how to handle exceptions in Python.

Fix method 3: validate user input

User input is a common cause of this error.

If the user enters 0, you can ask again or stop the program before dividing.

numerator = 100
divisor = int(input("Enter a divisor: "))

if divisor == 0:
    print("You must enter a number other than 0")
else:
    print(numerator / divisor)

If the user types something that is not a valid integer, int() can raise a different error. You can learn more about that in ValueError when parsing input in Python.

You may also find these helpful:

Debugging steps

If you are not sure where the zero came from, follow these steps.

1. Find the exact division line

Read the traceback and locate the line that failed.

Look for the line using:

  • /
  • //
  • %

2. Check the value on the right side

The right-side value is the divisor. That is the value you need to inspect.

For example:

numerator = 25
divisor = 5 - 5

print(numerator / divisor)

Here, the divisor is not written as 0, but the calculation produces 0.

3. Print values before the failing line

Add debug prints before the division:

numerator = 25
divisor = 5 - 5

print(divisor)
print(type(divisor))
print(f"numerator={numerator}, divisor={divisor}")

print(numerator / divisor)

Useful debugging commands:

print(divisor)
print(type(divisor))
print(f"numerator={numerator}, divisor={divisor}")
breakpoint()

4. Trace where the divisor was assigned

If the divisor comes from another part of the program, go back and check:

  • where the variable was assigned
  • whether a function returned 0
  • whether a loop changed the value
  • whether user input was converted into 0

Example with a function:

def get_divisor():
    return 0

number = 10
divisor = get_divisor()

print(number / divisor)

In this case, the real issue is inside get_divisor().

Operators that can raise ZeroDivisionError

These operators can all raise ZeroDivisionError when the second value is 0.

/ division

print(10 / 0)

Error:

ZeroDivisionError: division by zero

// floor division

print(10 // 0)

Error:

ZeroDivisionError: integer division or modulo by zero

% modulo

print(10 % 0)

Error:

ZeroDivisionError: integer modulo by zero

A common question is: what is the difference between preventing the error and catching the error?

  • Preventing the error means checking the divisor first with if divisor != 0
  • Catching the error means letting the division happen inside try, then handling ZeroDivisionError in except

For most simple programs, preventing the error is clearer.

Another common question is why Python raises an error instead of returning infinity. Python does this because division by zero is usually a mistake in program logic, and raising an error helps you find and fix it.

You may also need to decide what your program should return when zero is not allowed. That depends on the task. Common choices are:

  • show an error message
  • ask the user again
  • return a default value
  • stop the current calculation

FAQ

Can ZeroDivisionError happen with float values?

Yes. If the divisor is 0.0, Python still raises ZeroDivisionError.

print(5 / 0.0)

Does modulo by zero also raise this error?

Yes. Using % with 0 raises ZeroDivisionError.

print(7 % 0)

Should I always use try-except for this?

No. If you can check the divisor first, that is usually simpler and clearer.

How do I find where the zero came from?

Read the traceback, inspect the divisor variable, and trace where it was assigned or returned.

See also