10. Reading error messages

When a program is wrong, Python does not stay silent — it prints an error message and stops. Beginners see that red text as scary noise and start changing things at random. But it is not noise: Python is telling you exactly which line to look at and hinting at what is wrong. This chapter teaches you to read it.

The shape of an error

Almost every Python error has the same shape:

Traceback (most recent call last):
  File "file.py", line LINE, in <module>
    the line of code that broke
ErrorType: message

Four parts, and you read them in this order:

  1. The file name and line number (File "file.py", line 3). Look there first. Always.
  2. The line of code shown directly below it. Python quotes the actual source line so you can see it.
  3. The error type (ErrorType), like SyntaxError or NameError. This is the category of problem.
  4. The message — a short description of what went wrong.

The word "Traceback" at the top and the in <module> part are not important yet. Focus on the file/line and the last line of the error.

Four errors you will actually hit

These four cover most early mistakes. Learn to recognise them by sight.

SyntaxError: unterminated string literal

You forgot a closing quote.

print("hi)
  File "hello.py", line 1
    print("hi)
          ^
SyntaxError: unterminated string literal (detected at line 1)

"Unterminated string literal" means Python found an opening quote and reached the end of the line without finding the closing one. Fix: add the missing ".

NameError: name is not defined

You spelled a variable or function name wrong.

prnt("hi")
Traceback (most recent call last):
  File "hello.py", line 1, in <module>
    prnt("hi")
NameError: name 'prnt' is not defined

Python is saying: you used the name prnt, but nothing by that name exists. The name in quotes ('prnt') is the misspelled word. Fix: spell print.

IndentationError: unexpected indent

You added spaces at the start of a line that should not be indented.

print("hello")
    print("world")
  File "hello.py", line 2
    print("world")
    ^
IndentationError: unexpected indent

Python uses indentation to understand the structure of code. An extra indent where one is not expected is an error. Fix: remove the leading spaces from that line.

SyntaxError: expected ':'

You wrote an if, for, while, or def line but forgot the colon at the end.

if x > 3
    print("big")
  File "hello.py", line 1
    if x > 3
            ^
SyntaxError: expected ':'

This one is direct: Python expected a : to end the if line and did not find one. Fix: add the : at the end of the first line.

The edit–run–fix loop

The right way to deal with errors is not to spot them all by eye, but to run a tight loop:

  1. Run the file.
  2. Read the first error: line number, then error type, then message.
  3. Fix that one thing.
  4. Run again.

Repeat until it runs clean. Do not fix three things at once — one "fix" might be wrong, and then you cannot tell which change helped. One error, one fix, run again.

A single mistake sometimes produces a confusing message, or one that points at the line after the real problem. If the named line looks fine, check just above it.

Open exercises/10/01-three-bugs.py. It does not run. Use the edit–run–fix loop: run it, read the first error, fix only that, run again. Keep going until it prints All fixed!.

Errors are not failure

Every programmer reads error messages every day. They are not a sign you are bad at this — they are the normal back-and-forth of writing code. Reading errors calmly just means you have read a few thousand of them. You are on your way.

Homework

Homework files are in exercises/10/homework/.

Problem 1 — Name the error

Open exercises/10/homework/01-name-the-error.py. It contains one broken line. Run it, read the message, and (in a comment at the bottom) write which of the four error types from this chapter it is. Then fix it.

Problem 2 — Fix the quote

Open exercises/10/homework/02-fix-the-quote.py. It has an unterminated string literal. Fix it so the program prints the quote on one line.

Problem 3 — Fix the typo

Open exercises/10/homework/03-fix-the-typo.py. A function name is misspelled, giving a NameError. Fix it.

Challenge — Three in a row

Open exercises/10/homework/04-three-in-a-row.py. It has three separate mistakes, one of each: an unterminated string, a misspelled print, and an unexpected indent. Fix them one at a time, running after each fix, until the file prints:

done

Stuck or finished? Open the homework solutions page.