Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 0.7 — Reading Error Messages and Tracebacks
Python · Example 3 of 3

A traceback with several steps

Read the traceback from the bottom. Which line broke? Which line called it with an empty list? (def creates a named block of code; Phase 5 teaches it.)

When Python cannot run a line it stops and prints an error message. Read it from the bottom: the last line names the kind of error and what went wrong, and the lines above it (the traceback) show the file and line number where it happened.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

This program should print Total: 50 but crashes. Read the error, then fix it: line 1 price = 25, line 2 total = price * 2, line 3 print("Total: " + total).

02

Without running it, say what this prints, including the error type: line 1 print("A"), line 2 print(10 / 0), line 3 print("B").

03

A program fails with NameError: name 'totl' is not defined. Did you mean: 'total'? on line 7. What is the most likely mistake, and how do you fix it?

Explain it without notes

01

What is the difference between a syntax error and an exception?

02

In what order should you read a traceback, and what does each part tell you?

03

What does "most recent call last" mean?

04

Give the likely cause of each: NameError, TypeError, IndexError.

05

Why might the mistake not be on the line that the error reports?

A traceback with several steps
Sign in to run this example in your browser.

Expected output

Asha average: 75.0
Traceback (most recent call last):
  File "main.py", line 9, in <module>
    report("Ravi", [])
    ~~~~~~^^^^^^^^^^^^
  File "main.py", line 6, in report
    print(name, "average:", average(marks))
                            ~~~~~~~^^^^^^^
  File "main.py", line 3, in average
    return total / len(marks)
           ~~~~~~^~~~~~~~~~~~
ZeroDivisionError: division by zero