Reading An Error Message
The first time you get red text it feels like you have broken something. You have not.
An error message is the most useful output your program produces. It says what went wrong and where.
Every programmer sees errors daily — twenty years in, still daily. The difference is that an experienced one reads the message and fixes it in ten seconds.
Read it from the bottom
Traceback (most recent call last):
File "agent.py", line 12, in <module>
print(students[10])
IndexError: list index out of rangeThe last line is the answer. A type on the left, plain English on the right. Ninety per cent of the time that one line is enough.
The line above it is where it happened. Everything under Traceback is how you got there — ignore it while your code is one cell.
The five that happen in the first class
Not theoretical. Every one of these came up live.
1. Capital P
Print("hello")NameError: name 'Print' is not definedPython is case-sensitive. It is print.
2. A stray space
def add(a, b):
return a + b
print(add(4, 5))IndentationError: unexpected indentOne space, and Python thinks the line is inside a block.
3. Nothing happened at all
def add(a, b):
return a + bNo error. No output. Nothing.
Those lines define the function; they do not run it — see Defining, Calling And Blocks. Call it:
print(add(4, 5))If you got silence where you expected an answer, check you actually called the thing.
4. A missing colon
if number > 5
print("bigger")SyntaxError: expected ':'Every if, for, while, def and class line ends with a colon.
5. Text that looks like a number
age = "34"
print(age + 1)TypeError: can only concatenate str (not "int") to strThe quotes make it text. Use int(age) + 1.
The rest, briefly
| Message | Means | Fix |
|---|---|---|
NameError | that name does not exist | check spelling; check you ran the cell |
IndexError | that position does not exist | check len(my_list) |
KeyError | that key is not in the dictionary | use .get("key", default) |
AttributeError | no such method | check spelling and the object's type |
ModuleNotFoundError | library not installed | pip install thelibrary |
When SyntaxError blames an innocent line
scores = [90, 85, 72
print("done")It blames line 2. Line 2 is fine. The missing ] is on line 1 — Python kept reading and gave up on the next line.
When a SyntaxError makes no sense, look at the line above.
NameError for something you can see
You wrote the variable, it is right there on screen, and Python insists it does not exist.
You never ran that cell. Runtime → Restart and run all fixes it.
When stuck
Two minutes, in this order:
- Read the last line, out loud if it helps
- Go to the line number
- If that line looks fine, check the line above
- Print what you actually have:
print(type(x), x) - Paste the last line only into an AI chat
- Still stuck after five minutes — ask, and paste the error and the code
"It is not working" cannot be answered. The error plus five lines can be, in a minute.
The habit worth building
Do not change code at random until the red goes away. That occasionally works and teaches you nothing.
Say a sentence instead: "KeyError on 'city' means the dictionary has no city key, which means the response is shaped differently from what I assumed."
Once you can say it, the fix is obvious.
This is exactly the habit that makes AI-written code safe to use. When the AI's code breaks — and it will — you are the one reading the message.
Practise this
- Cause all five first-class errors on purpose. Fix each.
- Define a function, get nothing, then call it.
- Make a
KeyError, then fix it with.get()and with anif. - Take an error you do not understand and say in one sentence what you think it means, before looking it up.