One-sentence summary
An error message is Python's way of saying "the program stopped here, and this is why"; once you learn to read it, an error stops being a wall and becomes a clue that leads you to the fix.
Why does it matter?
Everyone who writes code gets errors. Beginners get them, and so do people who have been coding for years. The difference is what they do when the error appears. Some people see red text and panic, changing things at random. Others stop and read the message.
When Python reports an error, it is actually trying to help. It tells you where it stopped and what went wrong. If we learn to read this, every error turns into a small puzzle. In this lesson we will learn the parts of an error message and solve the five most common error types with examples.
Traceback: Python's error report
When a program hits an error and stops, Python prints a report to the screen. This report is called a traceback. Think of it as "tracing back" through the path Python took to reach the error.
Imagine you run this tiny program:
print(message)
Python prints a traceback similar to this:
Traceback (most recent call last):
File "program.py", line 1, in <module>
print(message)
NameError: name 'message' is not defined
This red text can look scary, but it contains only a few important parts.
The parts of a traceback
Traceback (most recent call last):This line just says "here comes the error report."File "program.py", line 1This tells you which file and which line the error was noticed on. Here it is line 1.print(message)Python shows you the code on that line, so you know where to look.NameError: name 'message' is not definedThe most important line. It has two parts: the error type (NameError) and the explanation (name 'message' is not defined).
Read it from the bottom up
The easiest way to read a traceback is to start at the bottom. The last line tells you what went wrong; the File ... line ... line above it tells you where to look.
So every traceback really answers two questions:
- What happened? → The error type and explanation at the very bottom.
- Where did it happen? → The line number next to the word
line.
Once you have those two pieces of information, you are already halfway to the fix.
Note: These tracebacks are from Python 3. Depending on the version, the arrows or small markers may look slightly different, but the error type and line number are read the same way.
Five common error types
When you are starting out, most errors come from a handful of types. Let us see each one with an example and a fix.
SyntaxError — a writing mistake
A SyntaxError means Python could not understand the sentence. Usually a mark is missing: a colon, a quote or a bracket.
if 5 > 3
print("Five is greater than three")
File "program.py", line 1
if 5 > 3
^
SyntaxError: expected ':'
Python expected a colon at the end of the if line but did not find one. The fix is to add : at the end of the line:
if 5 > 3:
print("Five is greater than three")
NameError — an undefined name
A NameError means a name you used was never defined. It is often a typo or a forgotten assignment.
score = 10
print(scroe)
Traceback (most recent call last):
File "program.py", line 2, in <module>
print(scroe)
NameError: name 'scroe' is not defined
The variable is called score, but we accidentally wrote scroe. The fix is easy: spell the name correctly.
TypeError — mismatched types
A TypeError appears when you try to use types that do not fit together. The classic example is trying to join a piece of text and a number with +.
age = 10
print("My age is " + age)
Traceback (most recent call last):
File "program.py", line 2, in <module>
print("My age is " + age)
TypeError: can only concatenate str (not "int") to str
Python can only add text to text. We first need to turn the number into text:
age = 10
print("My age is " + str(age))
IndexError — a position that is not in the list
An IndexError appears when you try to reach a position that does not exist in a list. In lists, counting starts at 0, and forgetting this is a common trap.
colours = ["red", "green", "blue"]
print(colours[3])
Traceback (most recent call last):
File "program.py", line 2, in <module>
print(colours[3])
IndexError: list index out of range
The list has 3 items, but their positions are 0, 1 and 2. The last item is colours[2]. The fix is to use a valid position.
ValueError — a wrong value
A ValueError appears when the type is right but the value itself is not suitable. A common example is trying to turn a piece of text that is not a number into a number with int().
number = int("hello")
Traceback (most recent call last):
File "program.py", line 1, in <module>
number = int("hello")
ValueError: invalid literal for int() with base 10: 'hello'
int() expected a number, but "hello" is not one. This error is common when taking a number from the user, so we need to make sure the value really is a number.
Fixing errors without fear
When you see an error message, you do not need to delete everything. A small, calm method works.
A three-step approach
- Read the bottom line. The error type and explanation tell you what went wrong.
- Go to the line number. If it says
File ... line 2, look at line 2 in your code. The error is usually right there or just above it. - Make one change and run again. Do not change many things at once; fix one, run it, and see the result.
Let us walk through an example:
def greet(name):
print("Hello " + name)
greet()
Traceback (most recent call last):
File "program.py", line 4, in <module>
greet()
TypeError: greet() missing 1 required positional argument: 'name'
The bottom line says it is a TypeError and that the name argument is missing. The line number points to line 4. We forgot to give a name when calling the function. One fix is enough:
def greet(name):
print("Hello " + name)
greet("Ada")
As you can see, the error message almost told us the solution directly.
Mini practice
The program below tries to print the square of a number but throws an error. Find and fix the error by reading the traceback.
number = input("Enter a number: ")
square = number * number
print("Square:", square)
- Run the program and note which error type you get.
- Read the bottom line of the traceback and the line number.
- Notice that the problem is that
input()returns text. - Write
number = int(input("Enter a number: "))to turn the text into a number, then try again.
To test your solution, enter different numbers. Then enter a letter and observe the kind of ValueError you get.
Common mistakes
Changing code without reading the message
Seeing red text and panicking into random changes usually makes things worse. Stop first and read the bottom line.
Looking at the wrong line
Sometimes the error says line 5 while the real problem starts on line 4. If you cannot see anything wrong on the line named, look at the line just above it too.
Ignoring the error type
A NameError and a TypeError are very different problems. Trying to fix one without reading the type wastes time. The type tells you what kind of problem you are looking at.
Forgetting that lists count from 0
The last position of a three-item list is 2, not 3. If you get an IndexError, this is usually why.
Safety note
While solving errors, do not blindly run code you copied from the internet; only run your own code that you understand and trust. Do not put personal details such as your name, address or password inside your programs; they could appear on screen with an error. When unsure, ask an adult.
Review questions
- Which part of a traceback should usually be read first?
- What is the difference between a syntax error and a runtime error?
- Why is changing several lines at once a poor debugging strategy?
- How can a minimal reproducible example help?
- What evidence should be included when asking for help with an error?
- Why should an AI-generated fix be tested rather than copied blindly?
Answers
- Start with the final exception line for the error type and message, then trace back to the first relevant line in your own code.
- A syntax error prevents Python from parsing the program; a runtime error occurs after execution has begun.
- It becomes impossible to know which change solved the problem or introduced a new one.
- Removing unrelated code isolates the failing behaviour and makes the cause easier to reproduce and explain.
- Include the exact error, relevant code, input, expected result, actual result, Python version and steps already tried.
- The suggestion may misunderstand the context, introduce insecurity or only hide the symptom; tests must confirm the intended behaviour.
Lesson summary
- A traceback is the report Python prints when it hits an error, and it is written to be read.
- Read a traceback from the bottom up: the last line says what happened, the
linenumber says where. - The error type tells you the kind of problem: SyntaxError, NameError, TypeError, IndexError, ValueError.
- Most errors have one small cause; making a single change and running again is the best method.
- Making mistakes is not failure; it is a normal part of learning.
Check your understanding
- What is a traceback, and in which direction is it easiest to read?
- Which part of a traceback tells you where the error is?
- What is the most likely cause of
NameError: name 'scroe' is not defined? - Why does
"My age is " + 10cause aTypeError, and how do you fix it? - Why does
list[3]cause anIndexErroron a three-item list?
Answers
- A traceback is the report Python prints when it meets an error and stops. It is easiest to read from the bottom up, because the last line tells you what went wrong.
- The
File "..." , line ...line tells you which file and which line the error was noticed on; that is, it shows you where to look. - It is most likely a typo. The real name of the variable is different (for example
score), and we misspelled it or forgot to define it. - Python can only add text to text, not a number. We fix it by turning the number into text with
str(10). - Because positions in a list start at 0. The three items have positions 0, 1 and 2; there is no position 3, so Python says "list index out of range."
Source and verification note
For “Reading Error Messages”, verification focuses on whether the relationship between Traceback: Python's error report and Read it from the bottom up remains consistent across examples. Code examples follow Python 3 syntax. Small differences may appear between environments, so examples should first be tested in a safe online editor or a local development setup.
Next lesson
Getting Started with Files: By learning to write information to a file and read it back, we make what our programs remember last beyond a single run.