Runtime errors in Python

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

Runtime errors in Python

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which error does this code cause? print(score) (score was never created)
A ZeroDivisionError
B NameError
2. What happens when this code runs? age = int("hello")
A ValueError: int() cannot turn the text into a whole number
B SyntaxError: the brackets are missing
3. Which line causes a ZeroDivisionError?
A print(0 / 10)
B print(10 / 0)
4. True or false: A runtime error is found only while the program is running.
A True
B False
5. When a program crashes with an error message, what should you read first?
A The error name and the line number
B Nothing, just run it again

Match the Pairs

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

Left columnRight column
1NameError: name 'score' is not defined AThe variable is spelled nmae in print(), but it was created as name. This causes a NameError. Fix it by typing print(name).
2ZeroDivisionError: division by zero BThe program tried to divide a number by 0, so it stopped running
3A ValueError happens when int() gets text that is not a whole number Cage = int("hello")
4A runtime error stops the program at the line that fails, so later lines never run Dprint("Start") x = 10 / 0 print("End")
5name = "Sam" print(nmae) EThe program used a variable name that was never created, or it was spelled differently
6total = 50 share = total / 0 FDividing by 0 causes a ZeroDivisionError. Fix it by dividing by a number that is not 0, such as total / 5.

Definitions

runtime error

Fill in the Blanks

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

1.
score = 10 print(_____)
2.
word = "cat" num = int(word) # causes a __________
3.
people = 0 if ______ != 0: print(10 / people)
4.
print(total) # total was never made: _________
5.
# This line causes a ZeroDivisionError print(10 / _)

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A players = 4
B pizzas = 2
C total = players + pizzas
D print("Total:", totl)
Question 2
A points = 20
B average = points / 0
C print(average)
Question 3
A age = int("twelve")
B next_age = age + 1
C print("Next year:", next_age)
Question 4
A name = "Mia"
B score = 10
C print(Mia, "scored", score)
Question 5
A total = 30
B count = 5
C mean = total / (count - 5)
D print(mean)

Answer Key

Runtime errors in Python
Section A: Quick Questions
Q1
B — NameError
Q2
A — ValueError: int() cannot turn the text into a whole number
Q3
B — print(10 / 0)
Q4
A — True
Q5
A — The error name and the line number
Section B: Match the Pairs
Q1
NameError: name 'score' is not defined → The program used a variable name that was never created, or it was spelled differently
Q2
ZeroDivisionError: division by zero → The program tried to divide a number by 0, so it stopped running
Q3
A ValueError happens when int() gets text that is not a whole number → age = int("hello")
Q4
A runtime error stops the program at the line that fails, so later lines never run → print("Start") x = 10 / 0 print("End")
Q5
name = "Sam" print(nmae) → The variable is spelled nmae in print(), but it was created as name. This causes a NameError. Fix it by typing print(name).
Q6
total = 50 share = total / 0 → Dividing by 0 causes a ZeroDivisionError. Fix it by dividing by a number that is not 0, such as total / 5.
Section C: Definitions
Q1
runtime error — An error that stops a program while it runs, shown as a named error like NameError, ValueError or ZeroDivisionError. Read the message to find the problem.
Section D: Fill in the Blanks
Q1
score = 10 print(score)
Q2
word = "cat" num = int(word) # causes a ValueError
Q3
people = 0 if people != 0: print(10 / people)
Q4
print(total) # total was never made: NameError
Q5
# This line causes a ZeroDivisionError print(10 / 0)
Section E: Spot the Error
Q1
D — The variable totl was never created, so Python stops with a NameError. The name must match the variable made earlier: total.
Q2
B — You cannot divide by zero. Python stops with a ZeroDivisionError. Divide by a number that is not zero, such as 4.
Q3
A — int() can only turn digit text like "12" into a number. The word "twelve" causes a ValueError. Use int("12").
Q4
C — Mia has no quote marks, so Python thinks it is a variable name that does not exist. This is a NameError. Use the variable name instead of Mia.
Q5
C — count - 5 is 0, so the program divides by zero and stops with a ZeroDivisionError. It should be total / count.