Sub topic 1
Read the Error Before You Ask
Prerequisites
This chapter assumes you can write and run basic Python, use a terminal or IDE, and have chatted with an AI assistant before. You do not need any debugging experience.
Why read the error first
An AI assistant answers the question you give it. If you paste an error without understanding it, you cannot tell whether the answer fits the evidence. Reading the error first takes about a minute. It lets you write a sharper prompt and catch a wrong diagnosis later.
Most error output answers three questions:
- What kind of error happened?
- Where in the code did it happen?
- How did the program get there?
Anatomy of a Python traceback
A traceback is the report Python prints when an exception goes uncaught. It lists the chain of function calls that led to the failure. Here is a small function that fails:
def average(values):
return sum(values) / len(values)
print(average([]))Running it prints this traceback:
Traceback (most recent call last):
File "stats.py", line 5, in <module>
print(average([]))
File "stats.py", line 2, in average
return sum(values) / len(values)
ZeroDivisionError: division by zeroRead it in this order:
- Last line.
ZeroDivisionError: division by zerogives the error type and a short message. The type is the category of failure. The message adds detail. - Frame above it. A frame is one entry in the call chain. The last frame,
File "stats.py", line 2, in average, is where Python stopped. The code on the next line issum(values) / len(values). - Frames above that. They show the callers, from oldest to newest. Here, line 5 of the module called
average([]).
The diagram shows the same chain.
The failing line is only where the program stopped. The cause may be earlier. Here, len(values) is 0 because the caller passed an empty list. The line is correct for normal input, so the real question is whether an empty list should be allowed.
Common error types
Learning a few types lets you form a first guess before any prompt.
| Error type | Typical meaning |
|---|---|
| `TypeError` | An operation got a value of the wrong type, such as adding a string to an integer |
| `ValueError` | The type is right but the value is not acceptable, such as `int("abc")` |
| `KeyError` | A dictionary lookup used a key that is not present |
| `IndexError` | A list index is outside the list |
| `AttributeError` | An object has no attribute of that name, often because it is `None` |
| `ImportError` | A module or name could not be imported |
| `ZeroDivisionError` | A division or modulo by zero |
Note your own hypothesis
Before you ask, write one sentence: "I think the cause is ..." For the example above, it might be "average is called with an empty list." Keep this sentence. In later chapters you will compare it with the assistant's diagnosis. If the two disagree, you have found something worth checking.
Other places errors hide
Not every failure is a traceback. Look for these as well:
- Test output, which shows the expected and actual values side by side.
- Warnings printed before the real failure.
- A process exit code that is not 0.
- Log lines just before a crash.
- Wrong output with no error at all. In that case, you must describe the expected and actual behavior yourself.
Copy the full error text, not your summary of it. A paraphrase like "it crashes on division" drops the file name, the line number, and the exact message that the assistant needs.
Exercise
A traceback ends with KeyError: 'email' and the lowest frame in your code is File "users.py", line 14, in build_profile. What do you know from this alone, and what is your first hypothesis?
Show answer
You know the error type (KeyError), the key that was missing ('email'), and the place where the lookup failed (line 14 of users.py, in buildprofile). A reasonable first hypothesis is that the dictionary passed to buildprofile has no 'email' key. The cause may be earlier, in the code that built that dictionary, so check what the caller passed before editing line 14.