// tech tips · beginner

How to Read Error Messages (and Actually Fix Them)

Error messages are clues, not failures. Learn how to read a Python traceback, what the most common errors mean, and a simple step-by-step method for debugging any program.

Every programmer, from complete beginners to people with twenty years’ experience, sees error messages every single day. The difference is that experienced developers read them. An error message is the computer telling you exactly what went wrong and usually where. Once you know how to read one, most bugs take minutes to fix instead of hours.

Anatomy of a Python error

Here’s a small program with a bug:

python
def average(numbers):
    return sum(numbers) / len(numbers)

scores = []
print(average(scores))

Running it produces a traceback:

output
Traceback (most recent call last):
  File "stats.py", line 5, in <module>
    print(average(scores))
  File "stats.py", line 2, in average
    return sum(numbers) / len(numbers)
ZeroDivisionError: division by zero

Read it from the bottom up:

  1. The last line tells you what happened: a ZeroDivisionError, because we divided by zero.
  2. The lines just above it tell you where: line 2, inside the function average.
  3. Further up shows how you got there: line 5 called average.

So the fix is clear: the list was empty, so len(numbers) was 0. We can guard against it:

python
def average(numbers):
    if len(numbers) == 0:
        return 0
    return sum(numbers) / len(numbers)

The errors you’ll see most often

Error What it usually means Typical fix
SyntaxError Python can’t understand the code at all Check for missing brackets, quotes or colons near the line shown
IndentationError Spaces at the start of a line are wrong Line up the block with four spaces; don’t mix tabs and spaces
NameError You used a name Python doesn’t know Check spelling and capitals; make sure the variable was created first
TypeError You used the wrong type of value Often text vs numbers. Convert with int() or str()
ValueError Right type, wrong value e.g. int("abc"). Check the input before converting
IndexError List position doesn’t exist Lists start at 0; the last item of 3 is at index 2
KeyError Dictionary key doesn’t exist Check the key’s spelling, or use .get()
AttributeError That value doesn’t have that method e.g. calling .append() on a string

A step-by-step debugging method

When something goes wrong, work through these steps in order.

1. Read the whole message, slowly. Don’t just glance at the red text and panic. Note the error type and the line number.

2. Go to the line and look one line above it. Missing brackets and quotes are often reported on the line after the real mistake.

3. Check your assumptions with print. If a value isn’t what you expect, print it just before the line that fails:

python
print("DEBUG scores =", scores, type(scores))

Seeing the actual value and its type solves a surprising number of bugs straight away.

4. Make the problem smaller. Comment out parts of the program, or copy the failing lines into a new file, until you find the smallest piece of code that still breaks.

5. Explain it out loud. Describe what each line should do to a friend, or even to a rubber duck on your desk. Programmers call this rubber-duck debugging, and it works because explaining forces you to notice the step you skipped.

6. Search the exact message. Copy the last line of the error (leave out your own file names and variable names) into a search engine. You are almost never the first person to see it.

Warnings vs errors

An error stops your program. A warning lets it run but says “this looks suspicious”. Don’t ignore warnings: they often point to a bug that will bite later.

The right mindset

Getting errors doesn’t mean you’re bad at coding. It means you’re coding. Each error you fix is one you’ll recognise instantly next time. Keep a note of the ones that took you longest, and you’ll be amazed how quickly that list stops growing.

Next, learn how to catch errors in your own programs with Python try and except, or test whether you can spot bugs in the Errors & Exceptions world of PyQuest.