Kahibaro
Discord Login Register

Reading error messages

Why Reading Error Messages Matters

Error messages are not just “red scary text.” They are Python’s way of telling you:

Learning to _read_ them calmly and systematically will save you huge amounts of time.

In this chapter you’ll learn how to:

The Structure of a Python Error (Traceback)

When Python hits an error while running a program, it prints a traceback. For beginners, it looks overwhelming, but it actually follows a predictable structure.

Run this code in a file to see an example:

print("Start")
x = 10 / 0
print("End")

You’ll see something like:

Start
Traceback (most recent call last):
  File "example.py", line 2, in <module>
    x = 10 / 0
ZeroDivisionError: division by zero

Let’s break this down.

The “Traceback” Header

Traceback (most recent call last):

This line simply means: “I’m about to show you the chain of steps that led to the error, starting from the most recent one.”

You don’t “fix” this line; it’s just announcing the error report.

File Name and Line Number

Example:

  File "example.py", line 2, in <module>
    x = 10 / 0

This tells you:

When debugging, this is usually the first thing you should look at:

  1. Which file?
  2. Which line?
  3. What does that line of code look like?

The Error Type and Message

The last line of the traceback is the key:

ZeroDivisionError: division by zero

It has two parts:

  1. Error typeZeroDivisionError
    This is the kind or category of problem.
  2. Error messagedivision by zero
    A short description of what exactly went wrong.

The error type is extremely helpful when searching online or in documentation:

A Step-by-Step Way to Read Any Error

A practical checklist when you see an error:

  1. Scroll to the bottom of the error
    The last line shows the error type and message.
  2. Read the file name and line number above it
    That’s where Python finally gave up.
  3. Open that file and go to that exact line.
  4. Read the code around that line
    Often the real mistake may be on that line or just above it.
  5. Use the error type as a hint
    • NameError → something wrong with a variable or name
    • TypeError → using values of incompatible types together
    • SyntaxError → your code is not valid Python syntax
    • etc.
  6. If you still can’t see it:
    • Cut the code down to a smaller example.
    • Add print() statements or use a debugger (covered in other sections).

Common Error Types and Their Messages

You will see some error types over and over. Here are several, focusing on how to read and interpret the messages rather than fully explaining the underlying concepts.

`SyntaxError` and `IndentationError`

These errors mean: “Python can’t understand your code; it’s not valid syntax.”

Example:

if True
    print("Hello")

Traceback:

  File "example.py", line 1
    if True
           ^
SyntaxError: expected ':'

How to read this:

Sometimes the real mistake is just before the arrow.

Another common one:

  File "example.py", line 2
    print("Hello")
        ^
IndentationError: expected an indented block

This tells you:

`NameError`

“Python found a name (usually a variable or function) that it doesn’t know.”

Example:

print(total)

Traceback:

  File "example.py", line 1, in <module>
    print(total)
NameError: name 'total' is not defined

Read the message:

Check:

`TypeError`

“Python doesn’t know how to perform this operation with these types.”

Example:

age = "20"
print(age + 5)

Traceback:

  File "example.py", line 2, in <module>
    print(age + 5)
TypeError: can only concatenate str (not "int") to str

Read the message:

The message usually tells you:

Another example:

len(5)

Traceback:

TypeError: object of type 'int' has no len()

That line tells you: “You can’t call len() on an int.”

`ValueError`

“The type is okay, but the value is not.”

Example:

age = int("twenty")

Traceback:

  File "example.py", line 1, in <module>
    age = int("twenty")
ValueError: invalid literal for int() with base 10: 'twenty'

How to read:

So:

`IndexError` and `KeyError`

These are about accessing things that don’t exist in collections.

`IndexError`

Lists (and similar sequences) use indexes.

Example:

numbers = [10, 20, 30]
print(numbers[5])

Traceback:

IndexError: list index out of range

The message tells you:

`KeyError`

Dictionaries use keys.

Example:

person = {"name": "Alice"}
print(person["age"])

Traceback:

KeyError: 'age'

The message tells you:

`AttributeError`

“You tried to access an attribute or method that doesn’t exist for this object.”

Example:

text = "hello"
text.push("!")

Traceback:

AttributeError: 'str' object has no attribute 'push'

Read the message:

The message tells you:

This is very useful for checking:

`ZeroDivisionError`

You’ve already seen this:

x = 10 / 0

Traceback:

ZeroDivisionError: division by zero

The message is very literal. You tried to divide by zero, which is not allowed.

Reading Multi-Line (Nested) Tracebacks

Sometimes your program calls several functions, and the error happens deep inside. The traceback will show several “File … line …” entries, one for each step in the call chain.

Example:

def divide(a, b):
    return a / b
def average(x, y):
    return divide(x + y, 0)
average(10, 20)

Traceback:

Traceback (most recent call last):
  File "example.py", line 7, in <module>
    average(10, 20)
  File "example.py", line 5, in average
    return divide(x + y, 0)
  File "example.py", line 2, in divide
    return a / b
ZeroDivisionError: division by zero

How to read this:

  1. Start from the bottom:
    • ZeroDivisionError: division by zero
    • The error actually occurred in divide, at return a / b.
  2. Move one step up:
    • File "example.py", line 5, in average
      return divide(x + y, 0)
      So average called divide with b = 0.
  3. Move up again:
    • File "example.py", line 7, in <module>
      average(10, 20)
      The top-level code called average.

In general:

Recognizing Errors in Different Environments

Error messages look slightly different depending on where you run Python:

No matter where you see it, the same rules apply:

Simple Habits for Using Error Messages Effectively

To get the most out of error messages:

  1. Don’t scroll past them
    Read them carefully; they are there to help, not to punish.
  2. Start at the bottom
    Look at the error type and message first.
  3. Use the line number
    Go directly to that line in your file and inspect that part of the code.
  4. Look a little above the line
    Sometimes the line above is where the real mistake is (especially for SyntaxError and IndentationError).
  5. Search for the error type + message
    For example:
    • python TypeError: can only concatenate str (not "int") to str
      You’ll usually find explanations and examples quickly.
  6. Copy the minimum code that shows the error
    If you ask for help, share:
    • The full error message (don’t just say “it doesn’t work”)
    • A small snippet of code that reproduces it

Practice: Reading a Few Error Messages

You can practice by writing code that you know will fail and then reading the error:

  1. NameError
print(total)
  1. TypeError
number = 5
text = "hello"
print(number + text)
  1. IndexError
values = [1, 2, 3]
print(values[10])

For each:

The goal is not just to “make the red go away,” but to understand what Python is telling you. Once you can read error messages confidently, debugging becomes much less frustrating and much more like solving a small, clear puzzle each time.

Views: 17

Comments

Please login to add a comment.

Don't have an account? Register now!