Phase 1: Prepare
Step 1 Warm-up recall
From Module 6: what should a program do with a bad row?
Private: nothing you type here is sent or stored.
Step 2 Learning goal
By the end of this lesson you can:
- read the last line of a traceback (Foundation);
- name the common errors (Core);
- find and fix the faulty line (Core).
Phase 2: Teach
Step 3 A familiar example
A warning light on a machine tells the engineer which part to check. A Python error message does the same: it names the problem and the line.
Step 4 Clear explanation
When Python cannot continue it stops and prints a traceback. Read it from the bottom.
prices = [4, 5, 6]
total = 0
for p in prices:
total = total + p
print(total / 0)Traceback (most recent call last):
File "prog.py", line 5, in <module>
print(total / 0)
ZeroDivisionError: division by zeroThe last line names the error and gives a reason. The line above shows the code that failed, and its number. Fix that line, or the cause that fed it, and run again.
| Error | Usual cause | Example |
|---|---|---|
| SyntaxError | the code is not valid Python | a missing bracket or colon |
| NameError | a name that does not exist | a misspelled variable |
| TypeError | the wrong kind of value | "5" + 2 |
| ValueError | right kind, unusable value | int("abc") |
| IndexError / KeyError | a position or key that is not there | items[9]; d["zz"] |
| ZeroDivisionError | division by zero | 10 / 0 |
A SyntaxError stops the program before it starts. The others are runtime errors that happen while it runs. A far worse case is a program that runs without any error and gives a wrong answer, which is why testing matters.
Step 5 Worked example (4 marks)
The last line of a traceback reads KeyError: "zz" and points to print(price["zz"]). What is wrong and how can it be fixed?
- KeyError means a dictionary key is missing. (1 mark)
- The key "zz" is not in price. (1 mark)
- Fix: check with in, or use price.get("zz", 0). (1 mark)
- Answer: a missing key, fixed by in or get. (1 mark)
Go deeper: the professor's notes
Error name.
The code.
The cause.
Names
| Error | Meaning |
|---|---|
| IndexError | position missing |
| KeyError | key missing |
At work: A crash report
A program stops with a traceback.
- Read the last line.
- Find the line number.
What a careful practitioner decides. Read the error before changing the code.
Phase 3: Practise
Step 6 Learner practice (instant-marked)
1. A traceback is best read from:
The last line names the error.
2. int("abc") causes:
Right kind, unusable value.
3. A misspelled variable name gives:
The name does not exist.
4. A program with no error message but a wrong total has:
Testing finds it.
Phase 4: Close
Step 7 Recap
- Read the traceback from the bottom.
- Learn the common error names.
- A wrong answer with no error is the hardest fault.
Step 8 Exit ticket (ungraded)
Name the error for items = [1, 2]; print(items[5]) and say why it happens.
Private: nothing you type here is sent or stored.
Step 9 Go further (Extension)
Write three short programs, each causing a different error, and name each error.
In industry: Therac-25: software trusted instead of hardware
Between June 1985 and January 1987 a computer-controlled radiation therapy machine, the Therac-25, massively overdosed six people. A race condition let a fast operator edit settings so a safety check was skipped, and the earlier machine's hardware interlocks had been replaced by software checks.
Link to this lesson. Do not let software alone guard against the worst outcome, and test with fast, unusual input from real users.
Source: Leveson and Turner, An Investigation of the Therac-25 Accidents, IEEE Computer (July 1993); Microsoft MSDN Magazine editor note (2016)
Common mistakes
| Mistake | Why it fails |
|---|---|
| Reading from the top | Start with the last line. |
| Changing code at random | Find the cause first. |
| Ignoring the line number | It shows where to look. |
Limits. Errors inside several nested function calls give longer tracebacks; read the last line and the last file line first.