Skip to content

Reading error messages

~15 min ยท prerequisite: Arithmetic

What you will be able to do

  • Find the error type, the message, and the line number in a Python error report.
  • Tell apart a syntax error, which stops the program before it runs, from a runtime error, which happens while it runs.
  • Fix an error by reading the line it points to and the message that explains it.

The idea

When Python cannot continue, it stops and prints an error report. Every report has the same structure, and once you know where to look, it tells you what went wrong and where.

Here is an example from the arithmetic lesson.

Traceback (most recent call last):
  File "division.py", line 1, in <module>
    print(10 / 0)
          ~~~^~~
ZeroDivisionError: division by zero

Read it in this order:

  1. The last line is the most important. It has the form ErrorType: message. Here the type is ZeroDivisionError and the message is division by zero. The type names the category of the problem, and the message explains it in words.
  2. The File line says where it happened. division.py is the file, and line 1 is the line number. Line numbers start at 1.
  3. The indented line below the File line shows the statement that was running.
  4. The marks under the code (^ and ~) point at the part that failed. Their exact shape varies between Python versions, so treat them as a hint.
  5. The whole block is called a traceback. Each File entry above the last line is a step that led to the error, with the most recent step last.

A syntax error is different. Python checks the whole file's grammar before it runs any line, so the program never starts. The report looks like this:

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

A runtime error happens while the program runs, so earlier lines may already have run. NameError and ZeroDivisionError are runtime errors.

Two habits help. Fix the first error in the report and run again, because one mistake can cause others below it. Then read the line the report points to, and check what the message says about it.

Python sometimes adds a hint such as Did you mean: 'print'?. Check that the hint matches what you meant.

You will learn try and except properly in Part 4. Here they only check message text.

Python version and error messages

The code and the error messages in this course were tested on Python 3.12 and 3.14. Error wording changes slightly between versions, so the message on your screen may not match the lesson word for word. That is normal. If your message differs, copy the code and the error into an AI assistant and ask it to explain the difference and give you updated code for your version.

Worked example

Here is a program with a mistake on the second line.

total = 4
print(totl * 2)

Line by line, this is what Python does:

  1. total = 4 creates a variable named total and stores 4 in it. This line runs without problems.
  2. print(totl * 2) tries to read a variable named totl. The program has no such name, because the first line created total. Python stops here.

The report is:

Traceback (most recent call last):
  File "example.py", line 2, in <module>
    print(totl * 2)
          ^^^^
NameError: name 'totl' is not defined. Did you mean: 'total'?

The type is NameError and the line is 2. The hint suggests total, so fix the spelling on line 2.

Try it

Each block causes an error on purpose, catches it with try and except, and checks the message with assert.

try:
    result = 10 / 0
except ZeroDivisionError as error:
    message = str(error)
assert message == "division by zero"
try:
    number = int("abc")
except ValueError as error:
    message = str(error)
assert message == "invalid literal for int() with base 10: 'abc'"
try:
    print(undefined_name)
except NameError as error:
    message = str(error)
assert "undefined_name" in message

Common mistakes

  1. Reading only the first line of the report. The first line says Traceback, which tells you nothing about the problem. Scroll to the last line.

  2. Fixing a different line from the one reported. A SyntaxError often points to the place where Python first noticed a problem, which can be later than the real cause. For example, a missing closing bracket is reported at the open bracket:

    print("a"
    x = 1
    
    SyntaxError: '(' was never closed
    

    Look at the bracket that was never closed, then add the ).

  3. Mixing a number and text. Writing print(2 + "3") gives:

    TypeError: unsupported operand type(s) for +: 'int' and 'str'
    

    The message says which two types cannot work together. Convert one of them.

  4. Using a name before it has a value. Writing c = b + 1 before b = 2 gives:

    NameError: name 'b' is not defined
    

    Move the line that creates b above the line that uses it.

Practice

  1. Read this report and write the error type, the message, and the line number in a comment.

    Traceback (most recent call last):
      File "report.py", line 3, in <module>
        print(score + 1)
    NameError: name 'score' is not defined
    
  2. The following program has one error. Find it, fix it, and run it. The final assert should pass.

    price = 5
    quantity = 3
    total = pric * quantity
    assert total == 15
    
  3. The following program stops with a TypeError. Explain in a comment why, and fix it so the assert passes.

    age = input("Age? ")
    assert age + 1 == 19
    

    Hint: this block does not run in the browser because it calls input. Run it in your terminal with 18 as the answer.

  4. Predict the error type for print(7 / 0). Then run it and compare.

  5. Stretch: write a program that uses try and except to catch a ValueError from int("twelve"), store the message in a variable named message, and assert that the message starts with invalid literal.
Solution
  1. The error type is NameError, the message is name 'score' is not defined, and the line is 3.

  2. The name pric is misspelled. Change line 3 to total = price * quantity. The assert then passes.

  3. The value of input is a string, so age + 1 tries to add a number to text. Convert it with int:

age = int(input("Age? "))
assert age + 1 == 19

With the answer 18, the assert passes.

  1. The error type is ZeroDivisionError, and the message is division by zero.

  2. python try: number = int("twelve") except ValueError as error: message = str(error) assert message.startswith("invalid literal")

Recap

  • The last line of an error report gives the error type and a message. The File line gives the file and line number.
  • A syntax error stops the program before it runs. A runtime error happens while the program runs.
  • Fix the first error in the report, read the line it points to, and then run again.

Sources