University of Free Knowledge
QA 76.73 · fol. 16

Reading the Traceback

Debugging is a repeatable method: read the traceback, locate the failing line, form a single hypothesis, and test it. · 11 min

Programs stop with errors. Every programmer's, every day — this is normal, not a sign you are doing it wrong. When Python stops, it does not leave you guessing: it prints a traceback, a short report of what went wrong and where. The skill that separates a stuck hour from a two-minute fix is not writing code that never breaks. It is reading that report calmly and following a fixed method: read the traceback, find the failing line, form one hypothesis, and test it. This folio is that method, made routine.

Guess before you learn

A program stops and prints this: `` Traceback (most recent call last): File "shop.py", line 3, in <module> print(totl) NameError: name 'totl' is not defined `` Which line should you read first?

THE DEPTH DIAL — the same idea, younger or deeper
9–12

9–12

A traceback records the chain of calls that led to an error, printed so the triggering error sits on the last line: its type and message. Reading bottom-up gets you the diagnosis fastest, then the line reference above it gives the exact location. Debugging from there is a disciplined loop, not luck. Locate the failing line. Form one hypothesis about the cause — a name misspelled, an index past the end, a string where a number was needed. Make the single change that hypothesis predicts, and run again. If the error is gone, you were right; if not, you have ruled that idea out cleanly. Changing one thing at a time is what keeps the result interpretable.

traceback

The report Python prints when a program stops with an error. Its last line names the error type and message; read it first.

LINE OF THE TRACEBACKWHAT IT TELLS YOUFile "shop.py", line 3where it failed — the file and line numberprint(totl)the exact code on that lineNameError: name 'totl' is not definedwhat went wrong — the error type and message
PLATE I One traceback, decoded. The last line is the diagnosis; the line above is the address.

So the reading is bottom-up. The last line has two parts: an error type, one of a small family of names, and a message in plain English. NameError: name 'totl' is not defined tells you a name was used that Python never bound — almost always a typo or a variable used before it was set. The line above, File "shop.py", line 3, is the address: open that file, go to line 3, and you are standing on the failing line. Diagnosis, then location. From there, one hypothesis and one change.

Read the traceback above and plan the fix — the steps fade as you master them

1
Read the last line first. What is the error type?
NameError
2
The message names the trouble. Which name is not defined?
totl
3
The line above is the address. What line number does it point to?
line 3
4
Form one hypothesis: 'totl' is a typo for the real variable 'total'. What name should replace 'totl'?
total
Retrieval Gate — answer before you continue 0 / 4

1.In what order should you read a traceback to diagnose it fastest?

2.NameError: name 'total' is not defined most likely means what?

3.In the traceback, what does File "shop.py", line 3 tell you?

4.In one sentence, what is the first thing you do when a program stops with a traceback?

The error type is a strong hint by itself, because each type comes from a small set of causes. A TypeError means an operation met the wrong kind of value — adding a string to a number, say. An IndexError means you reached past the end of a list or string. A ValueError means the type was right but the value was not allowed, like int('hi'). A ZeroDivisionError means a division by zero. Learn these on sight and the last line often names the fix before you have even found the line.

Why is this true?

Why change only one thing before running again, instead of fixing several suspects at once?

Because if you change three things and the error clears, you do not know which change fixed it — and one of the others may have added a new bug you cannot see yet. One change per run keeps a clean link between what you did and what happened, so every run teaches you something definite.

ERROR TYPEUSUALLY MEANSEXAMPLE THAT TRIGGERS ITNameErrora name used before it was defined, or misspelledprint(totl)TypeErroran operation on the wrong kind of value'age: ' + 7IndexErroran index past the end of a sequence[1, 2, 3][5]ValueErrorright type, but an unacceptable valueint('hi')ZeroDivisionErrora division by zero10 / 0
PLATE II The common error types and what each one usually points to.

Ink That Thinks — guess first; the answer draws itself.
Put the steps of the debugging method into the order you should follow them.

  1. read the last line of the traceback for the error type and message
  2. go to the file and line number the traceback names
  3. form one hypothesis about what is causing the error
  4. make a single change to test that hypothesis, then run again
Reorder, then commit.
PLATE III The four-step debugging method as an ordered routine — guess in graphite, truth in ink.
Retrieval Gate — answer before you continue 0 / 4

1.Match each snippet to the error type it raises.

'age: ' + 7
[1, 2, 3][5]
int('hi')
10 / 0

2.You have three ideas about why a program crashes. What is the disciplined next move?

3.A list has length 5. Its valid indices run from 0. What is the largest index you can use before an IndexError?

4.age = input() then print(age + 1) stops with a TypeError. What is the fix?

That is debugging as a method rather than a mood: read the traceback bottom-up for the error type and message, go to the line it names, form one hypothesis, change one thing, and run again. Each error type narrows the causes; each single change keeps the result honest. You now have the whole first arc of programming — statements in order, values and types, decisions and loops, functions, collections, the everyday scans, and a reliable way back when something breaks. Every larger program you write is these same parts, combined with more nerve and read with the same calm eye.

Practice — new ink and old, interleaved

1.What happens when this runs? `` s = 'cat' s[0] = 'b' ``

2.What does this print? `` nums = [5, 1, 8, 3] best = nums[0] for x in nums: if x > best: best = x print(best) ``

3.What does this print? `` a = 2 a = a + 3 print(a) ``

4.What number does print(6 + 6) show?

5.For word = 'CODE', what is the largest index you can use before an IndexError?

6.For s = 'HELLO', what is s[1:3]?

7.A traceback ends with TypeError: can only concatenate str (not "int") to str. What kind of mistake is this?

8.Why does if x = 5: raise an error where if x == 5: does not?

9.For s = 'ARCHIVE', what is s[0:4]?

10.What is nums after this runs? `` nums = [1, 2, 3] nums.append(4) ``

The Call Slip — search everything Ctrl·K / ⌘K