Reading an error message and traceback

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

Reading an error message and traceback

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A traceback says 'NameError: name score is not defined' on line 7. What does this mean?
A Python found a name on line 7 that was never created
B Python could not find line 7 in the file
2. Where should you start looking for a bug when you read a traceback?
A At the line number the error message gives
B At line 1 of the program, every time
3. Which error type does this code cause? print('Hello'
A SyntaxError
B NameError
4. Which error type does this code cause? print(total) (total was never created)
A TypeError
B NameError
5. Which error type does this code cause? print('Age: ' + 15)
A IndexError
B TypeError

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 list only has positions 0 and 1, so index 2 does not exist. Use names[1] to get the last item.
2File "game.py", line 12, in <module> BIndexError: list index out of range
3An IndexError means you asked for a list position that does not exist CA SyntaxError means the code breaks Python's grammar rules. Here a closing bracket is missing, so add ) at the end of the line.
4A TypeError happens when you combine values of the wrong types, like a string and an integer DPython met a variable name it has never seen, often from a typo or using it before it was created
5File "app.py", line 5 print("Hi" ^ SyntaxError: '(' was never closed EThe traceback line that shows which file and which line number to start looking for the bug
6names = ['Ava', 'Ben'] print(names[2]) IndexError: list index out of range FTypeError: can only concatenate str (not "int") to str

Definitions

Traceback (most recent call last):

Fill in the Blanks

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

1.
_________: name 'scor' is not defined
2.
age = 15 print("Age: " + age) _________: can only concatenate str
3.
colours = ["red", "blue"] print(colours[5]) __________: list index out of range
4.
print("Hello" ___________: '(' was never closed
5.
File "main.py", ____ 3, in <module>

Spot the Error

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

Question 1
A score = 10
B bonus = 5
C total = score + bonnus
D print(total)
Question 2
A for i in range(3)
B print(i)
C print('Done')
Question 3
A pets = ['cat', 'dog', 'fish']
B print(pets[0])
C print(pets[1])
D print(pets[3])
Question 4
A age = 15
B msg = 'Age: ' + age
C print(msg)
Question 5
A name = 'Sam'
B points = 3
C print(name)
D print(Points)

Answer Key

Reading an error message and traceback
Section A: Quick Questions
Q1
A — Python found a name on line 7 that was never created
Q2
A — At the line number the error message gives
Q3
A — SyntaxError
Q4
B — NameError
Q5
B — TypeError
Section B: Match the Pairs
Q1
NameError: name 'score' is not defined → Python met a variable name it has never seen, often from a typo or using it before it was created
Q2
File "game.py", line 12, in <module> → The traceback line that shows which file and which line number to start looking for the bug
Q3
An IndexError means you asked for a list position that does not exist → IndexError: list index out of range
Q4
A TypeError happens when you combine values of the wrong types, like a string and an integer → TypeError: can only concatenate str (not "int") to str
Q5
File "app.py", line 5 print("Hi" ^ SyntaxError: '(' was never closed → A SyntaxError means the code breaks Python's grammar rules. Here a closing bracket is missing, so add ) at the end of the line.
Q6
names = ['Ava', 'Ben'] print(names[2]) IndexError: list index out of range → The list only has positions 0 and 1, so index 2 does not exist. Use names[1] to get the last item.
Section C: Definitions
Q1
Traceback (most recent call last): — The start of Python's error report. It names the error type (like NameError) and the line number where it happened, so you know where to start looking for the bug.
Section D: Fill in the Blanks
Q1
NameError: name 'scor' is not defined
Q2
age = 15 print("Age: " + age) TypeError: can only concatenate str
Q3
colours = ["red", "blue"] print(colours[5]) IndexError: list index out of range
Q4
print("Hello" SyntaxError: '(' was never closed
Q5
File "main.py", line 3, in <module>
Section E: Spot the Error
Q1
C — The traceback would say NameError: name 'bonnus' is not defined, on line 3. The variable is spelled bonus, so the line should be total = score + bonus.
Q2
A — A SyntaxError is reported on line 1 because the colon at the end of the for line is missing. It should be for i in range(3):
Q3
D — The list has 3 items, so the valid indexes are 0, 1 and 2. Index 3 gives IndexError: list index out of range on line 4. It should be pets[2].
Q4
B — You can't add a string and an integer. Python gives a TypeError on line 2. Convert the number with str(age).
Q5
D — Python is case-sensitive. The variable is points, not Points, so the traceback shows NameError on line 4. It should be print(points).