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.
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).
Without running it, say what this prints, including the error type: line 1 print("A"), line 2 print(10 / 0), line 3 print("B").
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
What is the difference between a syntax error and an exception?
In what order should you read a traceback, and what does each part tell you?
What does "most recent call last" mean?
Give the likely cause of each: NameError, TypeError, IndexError.
Why might the mistake not be on the line that the error reports?
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