Read the Failure and Locate the Code
Reproduce one failure, separate ordinary output from its traceback, find the deepest relevant frame, and distinguish where execution stopped from where the cause began.
A program may print useful output and then stop with a traceback. The traceback is not a verdict on the whole program. It is evidence about the operation Python could not complete.
Consider a report built from three sensor records:
The west reading is text rather than a number. A run can end like this:
Preparing sensor report
Traceback (most recent call last):
File "report.py", line 24, in <module>
print(mean_from_records(records))
File "report.py", line 9, in mean_from_records
total = total + reading
~~~~~~^~~~~~~~~
TypeError: unsupported operand type(s) for +: 'float' and 'str'
The first line is ordinary program output. It proves that execution reached and
completed the first print() call. The lines beginning with Traceback are the
failure report. They describe what happened after that output; they do not erase
the work that already completed.
Begin with the Final Exception Line
Read the final line before reading every frame:
TypeError: unsupported operand type(s) for +: 'float' and 'str'
It contains an exception name and a message. The useful facts are that addition received a floating-point number and a string. The exact wording and the caret display can vary between Python versions, but the failed operation and involved types remain the same evidence.
The exception name is not a repair instruction. Converting every value, changing the addition, or catching the exception would be guesses until the program's intended data is clear. First locate the operation.
Q1. Separate output from the traceback
Which line proves that the program began running before it failed?
Select one choice, then check.
HintFind the boundary
The failure report starts at the word Traceback.
SolutionThe earlier print completed
Preparing sensor report came from the program's first print() call. The
traceback begins on the following line, so execution had already started.
Find the Deepest Relevant Frame
Each File ... entry and its source line form a traceback frame. A frame
records an active call when the failure occurred. Read the frames from the main
program toward the deepest relevant call:
| Frame | What it records |
|---|---|
report.py, line 24, in <module> | the main program called mean_from_records(records) |
report.py, line 9, in mean_from_records | the function tried to evaluate total + reading |
The second frame is deeper because the main program entered
mean_from_records, which then reached the failed expression. Start there. It
points to the exact operation that could not continue:
total = total + reading
A browser, notebook, library, or test runner may add frames around the code a learner wrote. Do not automatically edit the deepest environment frame. Find the deepest frame that points to the lesson, script, or function under your control. If your code passed an unsuitable value into a library, begin at that boundary rather than changing the library itself.
Q2. Choose the first source line to inspect
Which line is the deepest relevant location in this traceback?
Select one choice, then check.
HintFollow the active call
The main program entered mean_from_records before the failure occurred.
SolutionInspect the failed addition
The frame inside mean_from_records is the deepest reader-owned frame. Its
source line contains the float-plus-string operation described by the final
message.
Separate the Failure Location from Its Cause
The deepest frame tells us where Python stopped. It does not necessarily tell us where the unsuitable state began.
In this example, the addition is a reasonable operation when every available reading is numerical. The troublesome string entered earlier in the input:
{"sensor": "west", "reading": "24.0"}
Keep these questions separate:
| Question | Evidence in this run |
|---|---|
| Where did Python stop? | total = total + reading |
| Which value could not be used there? | the string "24.0" |
| Where did that value enter this computation? | the west sensor record |
Changing + to another operation would hide the symptom. Changing the west
record without understanding its source might repair only this example. The
next debugging step is to follow the values reaching the addition and find the
first state that breaks the program's assumption.
Q3. Distinguish location from cause
Which statement correctly separates the observed failure from its earlier cause?
Select one choice, then check.
HintTrace backward from the operation
The addition receives reading; inspect where that name got its value.
SolutionThe record precedes the failure
Python stops at total + reading, but reading refers to text because the
west input record supplied "24.0". The traceback location exposes an earlier
data problem.
Read ordinary output and the traceback as separate evidence. Begin with the final exception line, locate the deepest relevant source expression, and keep failure location separate from earlier cause. The next lesson inspects the values arriving at that expression and finds the first bad state.