Code Bytes · "Intro to Programming" · Episode 16 · Debugging
Reading an error message
When Python hits an error it stops and prints a traceback. Read the bottom line first for what went wrong (the error type and the reason), then the line number just above it for where.
A bug on purpose
Here's a tiny program with a bug in it. There are three snacks, and we ask for snacks[3]:
``python snacks = ["apple", "chips", "grapes"] print(snacks[3]) ``
Last episode we saw why that fails: the indexes of a three-item list are 0, 1 and 2, so there's no item three steps in. Save this as snacks.py, run it, and Python stops and prints this:
`` Traceback (most recent call last): File "snacks.py", line 2, in <module> print(snacks[3]) ~~~~~~^^^ IndexError: list index out of range ``
(Your computer prints the whole folder path in front of snacks.py; it's shortened here.) This red text is called a traceback: a trail back to the line that failed.
Read the bottom line first
The last line is the most useful one:
`` IndexError: list index out of range ``
- Before the colon is the error type:
IndexError. It names the kind of problem. - After the colon is the reason:
list index out of range. It explains it in plain-ish words.
So the bottom line tells you what went wrong: you asked for an index the list doesn't have.
Then look one step up
The lines above the bottom tell you where:
File "snacks.py", line 2, in <module>names the file and the line number: line 2.- The next line is a copy of that line:
print(snacks[3]). - The
~~~~~~^^^marks sit under the part that went wrong:snacks[3].
Notice that line numbers in a file start at 1, even though list indexes start at 0. The first line of your file is line 1.
Fix it
Now we know what (an index that doesn't exist) and where (line 2). Change the 3 to a 2, the last real index:
``python snacks = ["apple", "chips", "grapes"] print(snacks[2]) ``
`` grapes ``
Error messages aren't insults. They're clues, and the bottom line plus the line number usually take you straight to the problem.
Try it yourself
Open any free online Python editor (search "online Python") and run this broken program on purpose:
``python cookies = 12 print(cookies / 0) ``
You should see:
`` Traceback (most recent call last): File "main.py", line 2, in <module> print(cookies / 0) ~~~~~~~~^~~ ZeroDivisionError: division by zero ``
(The file name depends on your editor.) Read it the same way: the bottom line says what went wrong, ZeroDivisionError, because you can't share 12 cookies between 0 people. One step up, the line number says where. Now fix it by dividing by 6 instead, and it prints 2.0.
Good to know
- Every snippet on this page was run with Python 3 (3.13) and printed exactly what is shown, apart from the shortened file paths.
- The last line of a traceback always gives the error type first, then a short message. The lines above show where it happened.
- The
~and^marks under the failing part were added in Python 3.11. Older versions show only the copied line, but the line number is the same. - When code calls a function that calls another function, the traceback lists each step, with the most recent call last. That's why the bottom is the best place to start.
- Line numbers start at 1; list indexes start at 0. Easy to mix up, so check which one a message is talking about.
- Next episode: syntax vs logic errors, the bug that stops your code and the bug that quietly gives the wrong answer.
Answer to the comment question
Which line number will this error point to?
``python name = "Sam" print("Hi") print(nmae) ``
Line 3.
`` Hi Traceback (most recent call last): File "hello.py", line 3, in <module> print(nmae) ^^^^ NameError: name 'nmae' is not defined ``
Lines 1 and 2 run fine (line 2 even prints Hi). Line 3 asks for nmae, a misspelling of name, and Python has never heard of it, so the bottom line says NameError and the line number says line 3. Remember that lines count from 1, not 0. Some Python versions also add a hint such as Did you mean: 'name'?; the line number is the same either way.
Test your knowledge
Quick quiz: Reading an error message
4 multiple-choice questions. Pick one answer for each, then check how you did.
Sources
- Python Software Foundation, "Errors and Exceptions": Exceptions: the last line of the error message says what happened (the exception type, then details), and the part before it shows where, as a stack traceback with source lines
- Python Software Foundation, "Built-in Exceptions": IndexError: raised when a sequence subscript is out of range
- Python Software Foundation, "Built-in Exceptions": NameError: raised when a name is not found
- Python Software Foundation, "What's New in Python 3.11": Enhanced error locations in tracebacks: the ~ and ^ marks that point at the failing part of the line
Video transcript
Your code crashed with scary red text. Where do you look?
Here's a bug: three snacks, and we ask for snacks three.
Python stops and prints a traceback: a trail back to the line that failed.
Read the bottom line first. Before the colon is the error type, IndexError. After it, the reason: list index out of range.
One step up, it names the line number, line two, and shows that line, with marks under the problem.
So the bottom says what went wrong, and the line number says where. Change the three to a two, and it prints grapes.
Error messages aren't insults. They're clues.
Which line number will this error point to? Comment your answer below!
That's your byte.
More from "Intro to Programming"
- "Intro to Programming" EP11Infinite loops
- "Intro to Programming" EP12Functions
- "Intro to Programming" EP13Parameters and return values
- "Intro to Programming" EP14Lists and arrays