Skip to content
Free public beta until December 4, 2026: every course is open, no card needed. After that, the first chapter of each course stays free and full access is a one-time $39 ($19 for up to 100 founding members chosen for helpful feedback). Your progress is kept. How the beta works

Recipe

Read a traceback

Use when: a Python program stopped and printed a report

Goal: the line to fix and what is wrong with it

Taught in Input, output, and first errors

  1. 1

    Read the exception type and its message, and any Did you mean hint.

    Example

    NameError: name 'totl' is not defined. Did you mean: 'total'? says an unknown name, probably a typo for total.

    Why: the last line is the only one that says what went wrong; everything above says where.

  2. 2

    Go up one or two lines to the file name and line number, and the code with ^ marks under the failing part.

    Example

    File "main.py", line 3, in <module> and print("Total:", totl) with ^^^^ under totl.

    Why: the marks point at the exact part of the line, not just the line.

  3. 3

    No Traceback line and nothing printed: a grammar error, found before running. Otherwise it happened while running, at that line.

    • SyntaxError or IndentationError — fix the grammar; if the named line looks fine, check the line above it.
    • NameError or TypeError — look at the names and values used on that line.

    Why: grammar is checked for the whole file before any line runs; names and values only when the line runs.

  4. 4

    Change only what the report points at, then rerun to see the next report, if there is one.

    Why: one change at a time tells you which change fixed it.

Common mistakes

  • Reading from the top and stopping at Traceback (most recent call last):.
  • Ignoring the Did you mean suggestion.
  • Assuming the line named in a SyntaxError must be the broken one, when an unclosed bracket on the line above is the usual cause.