← VCE Software Development U3O1

Runtime error: divide by zero

A Python runtime error raised when a number is divided by 0, or a remainder is taken with % 0. Guard against it with an if check or try/except.

Understanding Runtime error: divide by zero in VCE Software Development U3O1

What it does: The if statement checks the divisor is not 0, so the division only runs when it is safe. The division is attempted in the try block, and the except block handles ZeroDivisionError and lets the program carry on. Dividing by zero is undefined, so Python stops with a ZeroDivisionError. It does not quietly return 0. Taking a remainder with % 0 is also a divide by zero. 0 % 17 is fine and gives 0 because the divisor is 17. Check that the divisor is not 0 before dividing. The second option is not valid Python syntax. Zero can come from a calculation, not just from user input. Here 5 - 5 is 0, so the division fails.

Common mistakes: The guard is inverted. The division now runs when n is 0 and causes a ZeroDivisionError. It should be if n != 0:. This divides by the constant 0, so it always raises ZeroDivisionError. It should divide by the checked variable n: print(100 / n).

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 156–166) and word search (Modules 142–166).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
What happens when Python runs the line: result = 10 / 0
It raises a ZeroDivisionError at runtimeIt sets result to 0
Matches
n = int(input('Divide by: ')) print(100 / n)
↓
The user could type 0, and the program never checks n before dividing, so it crashes with ZeroDivisionError. Add a check such as if n != 0.
Applying
Meanings
Term
ZeroDivisionError
Meaning
A Python runtime error raised when a number is divided by 0, or a remainder is taken with % 0. Guard against it with an if check or try/except.
Practice
total = 120
Testing
Gaps
except :
Check Answer
Spots
2n = int(input('People: '))
3if n == 0:

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Owl' badge — Wise Coder, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. What happens when Python runs the line: result = 10 / 0
A. It raises a ZeroDivisionError at runtime ✓
B. It sets result to 0
Dividing by zero is undefined, so Python stops with a ZeroDivisionError. It does not quietly return 0.
2. Which line causes a ZeroDivisionError?
A. remainder = 0 % 17
B. remainder = 17 % 0 ✓
Taking a remainder with % 0 is also a divide by zero. 0 % 17 is fine and gives 0 because the divisor is 17.
3. A user types 0 when asked for the number of people: average = total / people. When does the error appear?
A. Before the program starts, as a syntax error
B. At runtime, when the division line runs ✓
The code is valid Python, so it starts running. The error only appears when the line runs with people equal to 0.
4. Which is the best way to guard against zero coming from user input?
A. if people != 0: average = total / people ✓
B. average = total / people if people == 0
Check that the divisor is not 0 before dividing. The second option is not valid Python syntax.
5. Why can this code still cause a ZeroDivisionError? a = 5, b = 5, c = 10 / (a - b)
A. The calculation a - b produces 0, so the divisor is zero ✓
B. Python does not allow brackets in a division
Zero can come from a calculation, not just from user input. Here 5 - 5 is 0, so the division fails.

Match the Pairs

average = total / count → Raises ZeroDivisionError if count is 0, for example when the user enters 0 or a list is empty
remainder = 17 % 0 → Raises ZeroDivisionError because the remainder operator % also cannot use 0 as the divisor
Guard against dividing by zero with an if check before the division → if count != 0: avg = total / count
Catch a divide by zero error with try/except so the program does not crash → try: avg = total / count except ZeroDivisionError: avg = 0
n = int(input('Divide by: ')) print(100 / n) → The user could type 0, and the program never checks n before dividing, so it crashes with ZeroDivisionError. Add a check such as if n != 0.
if count == 0: avg = total / count → The check is the wrong way round. The division only runs when count is 0, which guarantees the error. It should be if count != 0.

Key Term

ZeroDivisionError
A Python runtime error raised when a number is divided by 0, or a remainder is taken with % 0. Guard against it with an if check or try/except.

Fill in the Blanks

1. try: avg = total / count except ZeroDivisionError:
Hint: Type the name of the exception that Python raises when a number is divided by zero.
2. if count != 0: avg = total / count
Hint: Type the comparison operator that checks the divisor is not equal to zero before dividing.
3. n = int(input("Divide by: ")) if n == 0: print("Cannot divide by zero")
Hint: Type the operator that tests whether the user's number is equal to zero.
4. divisor = 0 print(17 % divisor)
Hint: Type the operator that gives a remainder. Using it with a divisor of zero also causes this error.
5. try: ratio = a / (a - b) except ZeroDivisionError:
Hint: Type the keyword that starts the block which handles an error when a calculation produces a zero divisor.

Spot the Error

Question 1
A a = int(input('Total: '))
B n = int(input('People: '))
C if n == 0:
D print(a / n)
E else:
F print('N cannot be 0')
Line C — The guard is inverted. The division now runs when n is 0 and causes a ZeroDivisionError. It should be if n != 0:
Question 2
A nums = [4, 8, 0]
B for n in nums:
C if n != 0:
D print(100 / 0)
Line D — This divides by the constant 0, so it always raises ZeroDivisionError. It should divide by the checked variable n: print(100 / n).
Question 3
A def average(total, count)
B if count == 0:
C return 0
D return total / count
Line A — A function header must end with a colon. It should be def average(total, count):
Question 4
A try:
B print(100 / int(input()))
C except ZeroDivision:
D print('Zero not allowed')
Line C — ZeroDivision is not a real Python exception name. The correct name is ZeroDivisionError, so the divide by zero is never caught properly.
Question 5
A m = []
B if len(m) >= 0:
C avg = sum(m) / len(m)
D print(avg)
Line B — The check also passes when len(m) is 0, so the division by zero is not prevented. It should be if len(m) > 0:

Student with a class code? Join your class →