NameError: using a name Python does not know

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

NameError: using a name Python does not know

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line causes a NameError? score = 10 print(scor)
A score = 10
B print(scor)
2. What happens when this code runs? print(total) total = 5
A It prints 5
B NameError, because total is used before it is assigned
3. Which code runs without a NameError?
A name = 'Mia' print(name)
B name = 'Mia' print(Name)
4. Which code prints the word hello without a NameError?
A print('hello')
B print(hello)
5. True or false: Python treats age and Age as the same variable.
A False
B True

Match the Pairs

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

Left columnRight column
1print(scor) ANameError: the variable was spelt score, so Python does not know scor
2print(Hello) Bname = "Sam" print(Name)
3Capital letters matter, so Name and name are two different names CNameError: Hello has no quotes, so Python thinks it is a variable name
4A variable must be assigned before it is used, or Python does not know it yet Dtotal = 5 print(total)
5print(points) points = 10 EThe variable points is used on line 1 before it is assigned on line 2, so Python raises a NameError. Assign it first, then print it.
6colour = "blue" print(colur) FThe name is misspelt as colur, but the variable is colour. Fix the typo so both spellings match.

Definitions

NameError

Fill in the Blanks

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

1.
score = 10 print(_____)
2.
_____ = 5 + 2 print(total)
3.
Name = "Zoe" print(____)
4.
pet = "cat" print("My pet is", ___)
5.
for _ in range(3): print(i)

Spot the Error

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

Question 1
A name = Ava
B age = 12
C print(name)
D print(age)
Question 2
A score = 10
B score = scor + 5
C print(score)
Question 3
A colour = "blue"
B print("Sky is", Colour)
C print(colour)
Question 4
A def say_Hi():
B print("Hi!")
C say_hi()
Question 5
A name = "Zoe"
B age = 13
C print("Hello", name)
D print(Age, age)

Answer Key

NameError: using a name Python does not know
Section A: Quick Questions
Q1
B — print(scor)
Q2
B — NameError, because total is used before it is assigned
Q3
A — name = 'Mia' print(name)
Q4
A — print('hello')
Q5
A — False
Section B: Match the Pairs
Q1
print(scor) → NameError: the variable was spelt score, so Python does not know scor
Q2
print(Hello) → NameError: Hello has no quotes, so Python thinks it is a variable name
Q3
Capital letters matter, so Name and name are two different names → name = "Sam" print(Name)
Q4
A variable must be assigned before it is used, or Python does not know it yet → total = 5 print(total)
Q5
print(points) points = 10 → The variable points is used on line 1 before it is assigned on line 2, so Python raises a NameError. Assign it first, then print it.
Q6
colour = "blue" print(colur) → The name is misspelt as colur, but the variable is colour. Fix the typo so both spellings match.
Section C: Definitions
Q1
NameError — An error Python raises when code uses a variable or function name it does not know, often from a typo, wrong capitals, missing quotes or using a name before it is assigned.
Section D: Fill in the Blanks
Q1
score = 10 print(score)
Q2
total = 5 + 2 print(total)
Q3
Name = "Zoe" print(Name)
Q4
pet = "cat" print("My pet is", pet)
Q5
for i in range(3): print(i)
Section E: Spot the Error
Q1
A — Ava is text, so it needs quotes. Without them Python looks for a variable called Ava and raises a NameError. It should be name = "Ava".
Q2
B — scor is a misspelling of score. Python does not know a name called scor, so it raises a NameError. It should be score = score + 5.
Q3
B — Capital letters matter in Python. Colour is not the same name as colour, so this raises a NameError. It should be print("Sky is", colour).
Q4
A — The function is defined as say_Hi, but the call uses say_hi. Capital letters matter, so Python raises a NameError at the call. It should be def say_hi():.
Q5
D — Age is a label, so it needs quotes. Without them Python looks for a variable named Age and raises a NameError. It should be print("Age", age).