Specialty Digest

Global English editionLearners’ Study Dashboard — in development
The Knowledge Library for All

Flashcards: open the module flashcards (end-of-module review with workplace questions).

Online Courses / University Track / Python from Zero

Module 7 · Lesson 1 · Core

Reading error messages

Read a traceback from the bottom and name the common kinds of error.

  • 55 minutes
  • Serves O5
  • Assumes Module 6

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 zero
Readlast lineFindline numberLookat that lineFixrun again
Read an error from the bottom: the last line names the error, the line above it shows where it happened.

The 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.

ErrorUsual causeExample
SyntaxErrorthe code is not valid Pythona missing bracket or colon
NameErrora name that does not exista misspelled variable
TypeErrorthe wrong kind of value"5" + 2
ValueErrorright kind, unusable valueint("abc")
IndexError / KeyErrora position or key that is not thereitems[9]; d["zz"]
ZeroDivisionErrordivision by zero10 / 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?

  1. KeyError means a dictionary key is missing. (1 mark)
  2. The key "zz" is not in price. (1 mark)
  3. Fix: check with in, or use price.get("zz", 0). (1 mark)
  4. Answer: a missing key, fixed by in or get. (1 mark)

Go deeper: the professor's notes

Read the last line of an error first. It names the error, and the line above shows where.
Last line

Error name.

Line above

The code.

Fix

The cause.

Readlast lineNamethe errorFindthe lineFixand rerun
Read the report

Names

ErrorMeaning
IndexErrorposition missing
KeyErrorkey missing

At work: A crash report

A program stops with a traceback.

  1. Read the last line.
  2. 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:

2. int("abc") causes:

3. A misspelled variable name gives:

4. A program with no error message but a wrong total has:

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

MistakeWhy it fails
Reading from the topStart with the last line.
Changing code at randomFind the cause first.
Ignoring the line numberIt shows where to look.

Limits. Errors inside several nested function calls give longer tracebacks; read the last line and the last file line first.

Flashcards for this lesson (3)

Tap a card to reveal the answer. Read the question, answer it aloud or in writing first, then check yourself against the explanation.

Card 1 of 3 · How do you read a traceback?
Last lineerror nameAbovethe codeThenfix
Traceback

From the bottom. The last line names the error and gives a reason; the line above shows the code and its line number. Start there.

Card 2 of 3 · What is a SyntaxError?
Causebad formFoundbefore runFixthe code form
SyntaxError

The code is not valid Python, such as a missing bracket or colon. It is found before the program runs.

Card 3 of 3 · What is a NameError?
Causename not foundCheckspellingOrscope
NameError

Python cannot find a name, often because it is misspelled or was made inside a function. Check the spelling and the scope.

Review with flashcards

Test yourself on this module with workplace flashcards and detailed answers.

Open the module flashcards