Quiz
Warm-up from lesson 8-2: a function with no return statement is called and its result is printed. What appears?
The traceback is a map, not an insult
A working engineer spends a large share of every day reading error output, and the ones who read it calmly fix bugs in minutes instead of hours. Python's crash report is genuinely helpful once you know the reading order, so this lesson is about extracting the answer it is already giving you.
When Python hits something impossible, it stops and prints a traceback. Read it bottom-up:
Traceback (most recent call last): File "main.py", line 3, in <module> print(age + 3) TypeError: can only concatenate str (not "int") to str
- Last line first: the error type (
TypeError) and a plain-English message. - Line above it: the file, line number, and the exact code that failed.
The usual suspects:
| Error | Typical cause |
|---|---|
NameError | typo, or using a variable before assigning it |
TypeError | mixing types, like "17" + 3 |
ValueError | right type, bad content: int("hello") |
IndexError | lst[10] on a short list |
KeyError | dict lookup for a missing key |
ZeroDivisionError | dividing by 0 |
Quiz
Your program crashes with `ValueError: invalid literal for int() with base 10: 'twelve'`. Which line caused it?
Code exercise · python
This program crashes with `TypeError: can only concatenate str (not "int") to str` on line 2. Read the message, find the mismatch, and fix line 2 so it prints 20. (Hint: lesson 3-1 built the tool you need.)
Code exercise · python
One more crash to fix. This program stops with `NameError: name 'message' is not defined. Did you mean: 'mesage'?`. Read the message, spot the mismatch between the two lines, and make the program print its text. (Fix the typo in the variable definition, real names should be spelled correctly.)