Common Python Errors

Use exception names as a lookup aid while keeping the evidence-first method: inspect the failed operation, current values, earlier state, and the program's intended rule.

This page is a lookup aid, not a list to memorize. Begin with the operation that failed and the values that reached it. The exception name narrows the search, but the program's intended rule still determines the repair.

SyntaxError and IndentationError

Python could not read the program's structure, so execution did not begin. Inspect the marked line, then look slightly earlier for an unfinished expression or block.

Common first checks:

  • Is a quote, parenthesis, bracket, or brace left open?
  • Is a colon missing after if, else, for, while, or def?
  • Does the indentation describe the intended block?
  • Does the previous line end in an incomplete expression?

The reported location is a starting point. Python may notice an earlier missing delimiter only when it reaches the next line.

NameError and UnboundLocalError

Python could not find the value associated with a name at the point where the name was used.

Check the exact spelling and capitalization first. Then trace where the name should receive its value:

  • Has that assignment or function definition executed yet?
  • Can a branch or zero-iteration loop skip the assignment?
  • Is the name local to another function?
  • Does the current function assign to the name after trying to read it?

Prefer explicit parameters and return values when information must cross a function boundary.

TypeError

An operation or function call received values it could not use in that form. Inspect the operation, every supplied value and type, and the intended meaning.

Examples include adding a number to text, calling len on a number, supplying the wrong number of function arguments, or trying to place a mutable list in a set. Do not add a conversion merely to silence the error. Convert only when the converted value represents the same information the program needs.

ValueError

The kind of value was acceptable to the operation, but this particular value was not. For example, float("18.5") succeeds while float("missing") raises ValueError.

Inspect the exact value and the rule enforced by the operation. If invalid text is an expected boundary case and the program has a clear response, use one narrow handler. If a caller violates an explicit function rule, validation may raise ValueError directly instead.

IndexError

A sequence position lies outside the positions currently available. Before changing the index, inspect both the requested position and len(sequence).

For a non-empty sequence of length n, positive indexes run from 0 through n - 1. Empty sequences have no valid single-item index. When an index was calculated, trace that calculation backward; the lookup may only expose an earlier boundary mistake.

KeyError

A required dictionary key is absent. Print or otherwise inspect the requested key and the keys currently available. Check exact spelling, capitalization, and where the dictionary was built.

Use brackets when absence violates the data contract. Use membership or .get() only when absence is expected and the fallback has a defined meaning. Changing every required lookup to a default can hide missing data.

ZeroDivisionError

The divisor became zero. Inspect where that divisor was computed. A zero count often comes from empty input, all values being skipped, or a loop that did not run.

The right response depends on the contract. The mean(readings) function in this subject returns None when no reading is available; it does not invent a divisor or assert that input must be non-empty.

AttributeError

Python could not find the requested attribute or method on the current object. Inspect the object and its type, then check the attribute's exact name and the documentation for that type.

An attribute error often means the program holds a different kind of object than expected. Trace where that object was created or returned before changing the attribute name by guesswork.

Use the Same Method

Whatever name appears on the final traceback line, use the same evidence path:

  1. keep the input and reproduce the symptom;
  2. locate the deepest relevant operation;
  3. inspect the values and assumptions at that point;
  4. trace the first bad state backward;
  5. choose a policy and repair the cause;
  6. rerun the original case and keep a regression check.

The exception category shortens the search. It does not replace the search.

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.

Review

Not marked done.