Errors, Validation, and Debugging · practice
Read a Traceback from the Bottom
When an
Here is a small example:
def cost_per_guest(total_cost, guests):
return total_cost / guests
def show_summary():
amount = cost_per_guest(90, 0)
print(amount)
show_summary()
The traceback contains several lines, but you do not have to read it like a story from the top.
Start at the bottom
The final line says something like:
ZeroDivisionError: division by zero
It gives you:
the exception type:
ZeroDivisionError;the message:
division by zero.
That immediately tells you which operation failed.
Move upward to your code
Above the final line, the traceback points to the division inside cost_per_guest. Another entry shows that show_summary called the
This trail matters because the line where Python notices a problem is not always where the bad show_summary supplied the zero.
Read each relevant entry as:
This line was running, and it got here from the entry above it.
The reported line is evidence, not a verdict
Do not automatically edit the last highlighted line. First ask:
What values reached this operation?
Where did those values come from?
Which value violates the program’s requirement?
If zero guests is impossible in this program, the call is wrong. If zero is a possible real input, the program needs validation before it divides. Later lessons build that validation.
A reliable reading routine
Read the exception type and message at the bottom.
Move upward until you find lines from your program.
Identify the operation that failed.
Trace suspicious values back to where they were created or passed.
Make one justified change and run again.
Now trace that routine through the picture below. The program runs downward from caller to failure, while your reading starts at the numbered bottom step and works upward. Those opposite directions are the relationship to inspect.

The
Which part of a traceback should you usually read first?
The traceback ends with ZeroDivisionError: division by zero for this line:
return kilometers / hours
Which value should you trace back first?
Task
The starter program tries to show the average points per player, but it stops with an
Run it once and read the
The final program must print:
8.0
Keep the function and its division unchanged. The bug is in the value supplied by the call.