Course outline · 0% complete

0/32 lessons0%

Course overview →

Reading a Traceback

lesson 9-1 · ~11 min · 27/32

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:

ErrorTypical cause
NameErrortypo, or using a variable before assigning it
TypeErrormixing types, like "17" + 3
ValueErrorright type, bad content: int("hello")
IndexErrorlst[10] on a short list
KeyErrordict lookup for a missing key
ZeroDivisionErrordividing by 0
Traceback (most recent call last): File "main.py", line 3 print(age + 3)TypeError: can only concatenate…1. read this first:what went wrong2. then this:where it happened
Read a traceback bottom-up: the last line names the error, the lines above point to the exact file and line.

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.)