Runtime error: divide by zero

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Runtime error: divide by zero

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What happens when Python runs the line: result = 10 / 0
A It raises a ZeroDivisionError at runtime
B It sets result to 0
2. Which line causes a ZeroDivisionError?
A remainder = 0 % 17
B remainder = 17 % 0
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
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
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

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1average = total / count ARaises ZeroDivisionError if count is 0, for example when the user enters 0 or a list is empty
2remainder = 17 % 0 Bif count != 0: avg = total / count
3Guard against dividing by zero with an if check before the division Ctry: avg = total / count except ZeroDivisionError: avg = 0
4Catch a divide by zero error with try/except so the program does not crash DThe check is the wrong way round. The division only runs when count is 0, which guarantees the error. It should be if count != 0.
5n = int(input('Divide by: ')) print(100 / n) EThe 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.
6if count == 0: avg = total / count FRaises ZeroDivisionError because the remainder operator % also cannot use 0 as the divisor

Definitions

ZeroDivisionError

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
try: avg = total / count except _________________:
2.
if count __ 0: avg = total / count
3.
n = int(input("Divide by: ")) if n __ 0: print("Cannot divide by zero")
4.
divisor = 0 print(17 _ divisor)
5.
try: ratio = a / (a - b) ______ ZeroDivisionError:

Spot the Error

Shade in the letter of the line that contains an 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')
Question 2
A nums = [4, 8, 0]
B for n in nums:
C if n != 0:
D print(100 / 0)
Question 3
A def average(total, count)
B if count == 0:
C return 0
D return total / count
Question 4
A try:
B print(100 / int(input()))
C except ZeroDivision:
D print('Zero not allowed')
Question 5
A m = []
B if len(m) >= 0:
C avg = sum(m) / len(m)
D print(avg)

Answer Key

Runtime error: divide by zero
Section A: Quick Questions
Q1
A — It raises a ZeroDivisionError at runtime
Q2
B — remainder = 17 % 0
Q3
B — At runtime, when the division line runs
Q4
A — if people != 0: average = total / people
Q5
A — The calculation a - b produces 0, so the divisor is zero
Section B: Match the Pairs
Q1
average = total / count → Raises ZeroDivisionError if count is 0, for example when the user enters 0 or a list is empty
Q2
remainder = 17 % 0 → Raises ZeroDivisionError because the remainder operator % also cannot use 0 as the divisor
Q3
Guard against dividing by zero with an if check before the division → if count != 0: avg = total / count
Q4
Catch a divide by zero error with try/except so the program does not crash → try: avg = total / count except ZeroDivisionError: avg = 0
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
try: avg = total / count except ZeroDivisionError:
Q2
if count != 0: avg = total / count
Q3
n = int(input("Divide by: ")) if n == 0: print("Cannot divide by zero")
Q4
divisor = 0 print(17 % divisor)
Q5
try: ratio = a / (a - b) except ZeroDivisionError:
Section E: Spot the Error
Q1
C — The guard is inverted. The division now runs when n is 0 and causes a ZeroDivisionError. It should be if n != 0:
Q2
D — This divides by the constant 0, so it always raises ZeroDivisionError. It should divide by the checked variable n: print(100 / n).
Q3
A — A function header must end with a colon. It should be def average(total, count):
Q4
C — ZeroDivision is not a real Python exception name. The correct name is ZeroDivisionError, so the divide by zero is never caught properly.
Q5
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: