──────────────────────────────────────

57. Radio Shift Diagnostics · Trace the Running State

Inspect values before an assignment and fix a total that silently loses data.

Lesson files · lessons/debug-and-measure/trace-the-running-state

Before you start
- Follow a function call that takes a list and returns a number.
- Trace an assignment inside a for loop.

By the end
- Use trace output to find the assignment that loses the previous total.
- Show that a fixed running total works for a zero and an empty list.
- Use a local breakpoint, inspect a variable and tell stepping into a call from stepping over it.

Where you can use it
A station volunteer can find where the running time of a radio programme goes wrong even when the script finishes normally.

A program can finish normally and print a wrong answer. Calculate an expected result by hand, then record the difference. In Studio, the Before lines show the values just before each assignment, so you can find the first pass where the result goes off course.

On a computer with Python, run python3 -m pdb shift_total.py using the workbook file. A breakpoint pauses at a chosen line. Type p followed by a variable name to see its value. The arrow marks the next line to run. Use s to step into a call or n to reach the next line in the current function. The ZIP guide walks through the same bug in the terminal.

Predict
On the second pass, what will total be just after the assignment?
- 3
- 8

Worked example (example.py)

def total_minutes(durations):
    total = 0
    for minutes in durations:
        print(f"Before: total={total}, minutes={minutes}")
        total = minutes
    return total


print(f"Minutes: {total_minutes([5, 3])}")


Example result
Before: total=0, minutes=5
Before: total=5, minutes=3
Minutes: 3

Read space-separated whole-number clip durations. Run the starter and inspect the value of total just before each update. Fix the update so total accumulates every duration. Keep the Before lines and the final Minutes line. For an empty input, print only “Minutes: 0”.

Starter program (starter.py)

import sys


def total_minutes(durations):
    total = 0
    for minutes in durations:
        print(f"Before: total={total}, minutes={minutes}")
        total = minutes
    return total


durations = [int(value) for value in sys.stdin.readline().split()]
print(f"Minutes: {total_minutes(durations)}")


Starter input
5 3 2

Hint
The Before lines print prior state. Check whether the update uses both the previous total and the current minutes.

Case 1. Try the sample
Input
5 3 2
Expected output
Before: total=0, minutes=5
Before: total=5, minutes=3
Before: total=8, minutes=2
Minutes: 10

Case 2. A zero-minute placeholder at the end
Input
4 0
Expected output
Before: total=0, minutes=4
Before: total=4, minutes=0
Minutes: 4

Case 3. No clips
Input
(No input.)
Expected output
Minutes: 0

Stretch
Open README.txt next to shift_total.py in this lesson’s folder in the Python ZIP. Run python3 -m pdb shift_total.py. Follow the two pauses in the loop, then compare s and n at the call after making the fix.

The ZIP includes added practice files for this lesson. Open its lesson folder and read the included instructions.

Reflect
Which trace line gives the first evidence that the running total went wrong?

Check your prediction
3 The second Before line shows a total of 5. The assignment replaces it with the current duration of 3.
