← Back to course
Code Crew G6+ / Week 10 / Monday
1/6
Week 10 Β· Bugs and tests

Monday

Reading a long traceback
// Tracebacks, try and except, and assert
⏱ about 30 min

Monday: Reading a long traceback

Rocket's fuel report works fine for three days. On the fourth day the report is for a brand new ship with no fuel tanks yet. The shell fills with a long red traceback.

"Three different line numbers," he says. "Which one is the real problem?"

Raven reads the traceback from the top, then from the bottom. "What do you notice?" she asks. "Each line is one step of a trail. It goes from the line you ran, into a function, into another function."

Nova projects the trail as glowing footprints. "Let's debug together," she says. "Python lists the calls in order, oldest first. The last footprint is where it finally stopped."

Rocket starts at the bottom and follows the trail back up.

A traceback is a trail

# average_fuel.py
def average(numbers):
    return sum(numbers) / len(numbers)

def report(tanks):
    print("Average fuel:", average(tanks))

report([80, 60, 70])
report([])

The first report works. The second one stops with this traceback. Newer versions of Python also add lines of ^ marks under the trouble spot:

Average fuel: 70.0
Traceback (most recent call last):
  File "average_fuel.py", line 9, in <module>
    report([])
  File "average_fuel.py", line 6, in report
    print("Average fuel:", average(tanks))
  File "average_fuel.py", line 3, in average
    return sum(numbers) / len(numbers)
ZeroDivisionError: division by zero
  1. Read the last line: ZeroDivisionError, division by zero.
  2. Read the File line just above it: line 3, inside average. That is where it stopped.
  3. Walk up the trail: average was called from report on line 6, and report was called on line 9.
  4. Ask why: report([]) passed an empty list, so len(numbers) was 0.
In which function did the error happen?
Which line number started the trail in your own code (the <module> line)?
What is the error's name?
WHY DID IT FAIL?
  • Read the question.
  • Tap your answer.
What value did len(numbers) have when it failed?
At your computer
1. Type average_fuel.py, save it and run it.
2. Read the traceback from the bottom up. Find all three line numbers.
3. Click on the line report([]) in the shell with the right mouse button. Choose Go to file/line, if your IDLE offers it.
4. Change report([]) to report([50]) and run it again.

Practice: follow another trail

Here is a second trail to follow. This time the error is a KeyError, three calls deep.

# probe_battery.py
def battery_of(probe):
    return probe["battery"]

def show(probe):
    print(probe["name"], "battery:", battery_of(probe))

show({"name": "Probe 7", "battery": 87})
show({"name": "Probe 9"})
Probe 7 battery: 87
Traceback (most recent call last):
  File "probe_battery.py", line 9, in <module>
    show({"name": "Probe 9"})
  File "probe_battery.py", line 6, in show
    print(probe["name"], "battery:", battery_of(probe))
  File "probe_battery.py", line 3, in battery_of
    return probe["battery"]
KeyError: 'battery'
READ THE TRAIL
  • Read the question.
  • Tap your answer.
Which call worked before the error?
Why did the second call fail?
Which change would stop the crash and show 0 instead?
In which function did the error happen?
Which line number in <module> started the trail?
What key was missing?
PUT THE TRAIL IN THE ORDER PYTHON FOLLOWED IT
  • ?Line 9 calls show
  • ?Line 6 calls battery_of
  • ?Line 3 looks up the battery key
  • ?Python stops with a KeyError
WHY THIS EXERCISEEach call leads to the next, and the error happens at the newest one, at the bottom.
StatementTrue or false?
The last line of a traceback names the error.?
The first File line is where the error finally happened.?
Lines printed before the error still appear.?
A traceback can mention more than one function.?
WHY THIS EXERCISEThe error happens at the lowest File line, and the lines above show the calls that led there.

A long traceback is just a trail of calls, and the bottom tells you where to start. Tomorrow you will meet the most common errors and a bug that has no error message at all.