Chapter 7

Debugging Python Programs

A program may stop with a traceback or finish with the wrong answer. Follow connected measurement cases from symptom to evidence, repair, and a check that keeps the same defect from returning.

When a program stops, its traceback tells us where Python could no longer continue. When a program runs but prints the wrong number, there is no traceback to follow. In both cases, changing code by guesswork can hide the symptom without finding its cause.

We will use a connected set of small measurement cases throughout the chapter. First we locate an addition that received text instead of a number and trace that value backward. Then we investigate a mean that looks plausible but is wrong by comparing its intermediate state with a hand-worked case.

Once the cause is visible, the program still needs a policy. Some situations have a legitimate `None` result, some caller input should be rejected, some internal relationships should be asserted, and one expected conversion failure may be handled locally. The final lesson reduces the corrupted case, repairs its boundary, and keeps the original input as a regression check.

After this chapter

  • Reproduce a failure, read its final exception line, and locate the deepest relevant operation without confusing the failure site with its cause.
  • Inspect values, types, collection state, and intermediate results to find the first unsuitable value or broken assumption.
  • Debug a plausible wrong numerical answer by comparing a hand-worked case with the program's state after each step.
  • Choose deliberately among returning legitimate absence, rejecting invalid input, asserting an internal invariant, and handling one expected boundary failure.
  • Reduce a failing case without changing its cause, repair the responsible boundary, and rerun the original input.
  • Retain a regression check that would fail if the same defect returned.

Lessons

  1. 01
    Read the Failure and Locate the Code

    Read a traceback in a fixed order and locate the relevant operation without confusing the failure site with its earlier cause.

    3 exercises
  2. 02
    Trace Values and Assumptions

    Expose the first bad state with focused temporary diagnostics, trace its source, and remove the diagnostics after verification.

    3 exercises
  3. 03
    Debug a Wrong Answer

    Use a small expected result and a visible loop trace to find the first wrong initialization, update, or boundary.

    3 exercises
  4. 04
    Reject Invalid Input Clearly

    Validate threshold order near the boundary that understands it and raise a useful error without rejecting legitimate absence.

    3 exercises
  5. 05
    Check an Internal Assumption

    Assert that processed state cannot exceed examined state, using a useful message beside the update that can break the rule.

    3 exercises
  6. 06
    Handle One Expected Failure

    Report and skip one invalid numeric-text row without hiding failures the handler does not understand.

    3 exercises
  7. 07
    Reduce, Repair, and Verify

    Reduce one corrupted measurement case without changing its failure, then repair and verify the complete dataset.

    3 exercises

Review and practice

  1. Review

    Review the evidence-first debugging method and keep return, rejection, assertion, and expected recovery distinct.

  2. Exercises

    Practice the complete debugging path through ten cumulative tasks covering evidence, policy, repair, reduction, and verification.

Optional and reference

  1. Common Python Errors

    Look up concise first checks for common syntax, name, type, value, index, key, division, and attribute failures.

Chapter progress