0%

Errors, Validation, and Debugging · practice

Read a Traceback from the Bottom

When an stops a program, Python prints a . It is a trail showing which lines were active when the problem occurred.

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 originated. The division happens inside the function, but 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

  1. Read the exception type and message at the bottom.

  2. Move upward until you find lines from your program.

  3. Identify the operation that failed.

  4. Trace suspicious values back to where they were created or passed.

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

A traceback runs from caller to failed line and error, while the recommended reading order starts with the error at the bottom, then moves upward to the failed line and caller.

The shows the traceback when you run code. During grading, the same information may appear under the label Python error output.

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 from the bottom. Trace the invalid back to the call, then repair the call so the program models 48 total points shared by 6 players.

The final program must print:

8.0

Keep the function and its division unchanged. The bug is in the value supplied by the call.