Home · Academy · Robotics & Coding · Python Fundamentals · Reading Error Messages

Reading Error Messages

Learn to read Python error messages (traceback, error type, line) calmly and fix them.

LESSON COMPASS

What will you use this page for?

Core idea

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.

Evidence to produce

Complete the page task with your own input, test conditions and reasoning.

Control trap

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…

Next connection

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.

Module sources: Python Tutorial · Arduino Learn

LevelBeginner
Age10–16
Duration30–45 min
PrerequisiteFunctions
ContentStandard lesson · 1,711 words
Last updated

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

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:

  1. What happened? → The error type and explanation at the very bottom.
  2. 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

  1. Read the bottom line. The error type and explanation tell you what went wrong.
  2. 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.
  3. 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)
  1. Run the program and note which error type you get.
  2. Read the bottom line of the traceback and the line number.
  3. Notice that the problem is that input() returns text.
  4. 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

  1. Which part of a traceback should usually be read first?
  2. What is the difference between a syntax error and a runtime error?
  3. Why is changing several lines at once a poor debugging strategy?
  4. How can a minimal reproducible example help?
  5. What evidence should be included when asking for help with an error?
  6. Why should an AI-generated fix be tested rather than copied blindly?

Answers

  1. Start with the final exception line for the error type and message, then trace back to the first relevant line in your own code.
  2. A syntax error prevents Python from parsing the program; a runtime error occurs after execution has begun.
  3. It becomes impossible to know which change solved the problem or introduced a new one.
  4. Removing unrelated code isolates the failing behaviour and makes the cause easier to reproduce and explain.
  5. Include the exact error, relevant code, input, expected result, actual result, Python version and steps already tried.
  6. The suggestion may misunderstand the context, introduce insecurity or only hide the symptom; tests must confirm the intended behaviour.

Lesson summary

Check your understanding

  1. What is a traceback, and in which direction is it easiest to read?
  2. Which part of a traceback tells you where the error is?
  3. What is the most likely cause of NameError: name 'scroe' is not defined?
  4. Why does "My age is " + 10 cause a TypeError, and how do you fix it?
  5. Why does list[3] cause an IndexError on a three-item list?

Answers

  1. 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.
  2. The File "..." , line ... line tells you which file and which line the error was noticed on; that is, it shows you where to look.
  3. 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.
  4. Python can only add text to text, not a number. We fix it by turning the number into text with str(10).
  5. 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.

Start QuizBack to Python Fundamentals
QUESTION POOL

Reinforce this lesson with 10 questions

This lesson has a pool of 20 questions. Each attempt selects 10 and reshuffles the choices.