Read and Repair One Failure

Run the temperature program with one misspelled name, read the useful evidence in its traceback, and make a local repair. Rerun the complete program to verify that the expected report returns.

Not every run produces a report. If Python reaches an instruction it cannot carry out, it stops and displays an error. That error is evidence about where the run stopped and what Python could not do.

Introduce One Mistake

In the temperature program, change total to totl on the division line:

mean = totl / count

The name used in the first line remains total. Run this version and inspect what appears.

A misspelled name

Run the program with totl on the third line. Begin reading the error from its final line.

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

Python cannot find a remembered result named totl. It stops on the third instruction, before reaching any of the three print() calls. The temperature report therefore produces no ordinary output.

Read the Useful Part of the Error

The error ends with a line similar to this:

NameError: name 'totl' is not defined

NameError is Python's name for this kind of failure. The rest of the line identifies the particular name it could not find. We do not need a catalogue of error categories to repair this example. The quoted word totl is enough to direct our attention.

Above the final line, the traceback shows where Python was running when it stopped. In the browser it may include some lines belonging to the runner. Find the location that points to the code in the editor, then look for the line containing totl.

Python versions sometimes add carets or a suggestion such as “Did you mean?” Those details can help, but the repair should not depend on them. The source location and the final error line carry the stable evidence.

Repair the Cause, Then Run Again

The program creates total but later asks for totl. Restore the same spelling on the division line:

mean = total / count

Run the repaired program. It should again display:

total: 82
count: 4
mean: 20.5

A successful rerun matters. Editing the suspicious line is only a proposed repair; running the program again checks whether that repair restored the expected report.

Use a Small Repair Method

For this short failure, four questions are enough:

  1. Which instruction was Python trying to run?
  2. Which name does the final error line mention?
  3. Where else does the program create or use that result?
  4. Does one local spelling correction restore the expected output?

Later chapters develop a fuller method for failures and wrong answers. Here we need only enough error reading to continue working with our first program.

Q1. Repair a misspelled name

The program should display a total of 30, a count of 3, and a mean of 10.0. Run it, use the error to locate the misspelled name, make one repair, and run it again.

Editable Python

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

HintCompare the name with the first line

The first line remembers a result under total. The division line asks for a nearly identical but different spelling.

SolutionUse total on the division line

Replace totl with total. Python can then divide 30 by 3 and display the complete report.

Not attempted
Review

Not marked done.

A failed run is not an empty result. The traceback records where Python stopped, and its final line names the immediate problem. In this program, one misspelled name explains the failure, and a rerun verifies the repair. The next chapter will explain more precisely what Python names and values mean.

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 1 exercises marked done

Review

Not marked done.