Reading errors
Errors are clues, not failures. The last line names the type. For a runtime error, read the traceback's last frame from your code to find the line where it happened; earlier frames show how execution got there.
NameError: a name that doesn't exist (often a typo)TypeError: wrong type for the operation, e.g."a" + 1IndexError: index past the end of a listKeyError: a dict key that isn't thereValueError: right type, bad value, e.g.int("hi")
See it run
scores = [90, 85, 70] total = 0 for s in scores: total = total + s print("sum", total) print(scores[3])
Python runs top to bottom and stops at the first line that goes wrong.
Practice reading errors
Exercise 1
The program in the code editor below has three bugs. Fix them so it prints these three lines, in order:
I have 3 items, Last item: lamp, Done: 3.
Exercise 2
Repair the cafe summary in the code editor below without changing its source data. Convert the string quantity before multiplying, read the last order safely, and use the correctly spelled total. Print Cups: 6 then Last: tea.
Exercise 3
Fix the stock report in the code editor below: missing products should count as 0 without being inserted in stock. Use every requested name, keep their order, and print pen: 2, pad: 0, book: 1, then Total: 3. Repair the lookup, range boundary, and total update.
The explanation and tasks are public. Running this lesson requires an account with course access.
Practice
Loading Python…Visualizer Watch your code run line by line
Run your code, then choose Play to follow its execution.