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

61. Maintain a Python Tool · Reduce a Bug to a Small Example

Record the smallest input that still produces the reported error.

Lesson files · lessons/maintain-a-tool/reproduce-a-ticket

Before you start
- Filter a list of records by a value.
- Use strip() to remove whitespace from the ends of a string.

By the end
- Reduce the room records to the two rows that reproduce the problem.
- Record the input, the expected room count and the count the tool reports.

Where you can use it
Turn a report about duplicate room totals into a two-row example a colleague can run before touching the calculation.

A club report lists Hall and Hall with a trailing space as separate rooms. Start from the person’s input and write down the result they expected. Run the tool on those same rows and record what it actually shows.

Remove unrelated rows one by one while the issue still occurs. Keep both spellings of the affected room, including the space. The reduced input shows which values trigger the issue and gives you a small case to test.

Predict
Will removing the Hall row with the trailing space still reproduce two room totals?
- No. The remaining row gives one room total.
- Yes. The tool counts even one row twice.

Worked example (example.py)

def room_totals(rows):
    totals = {}
    for name, minutes in rows:
        totals[name] = totals.get(name, 0) + minutes
    return totals

rows = [("Hall", 6), ("Hall ", 4)]
print("Expected rooms: 1")
print(f"Reported rooms: {len(room_totals(rows))}")


Example result
Expected rooms: 1
Reported rooms: 2

Program input names the affected room. Build a reduced fixture that keeps only the rows whose room name matches after trimming spaces. Keep each retained row as written and run it through the supplied room_totals. Print the number of rows and reported rooms using the starter’s two lines. Record the expected and actual room counts in your notes.

Starter program (starter.py)

def room_totals(rows):
    totals = {}
    for name, minutes in rows:
        totals[name] = totals.get(name, 0) + minutes
    return totals

rows = [("Art", 5), ("Hall", 3), ("Art ", 8), ("Music", 9), ("Hall ", 2)]
target = input()
print(f"Rows: {len(rows)}")
print(f"Reported rooms: {len(room_totals(rows))}")


Starter input
Art

Hint
Use the trimmed room name for the filter. Pass the original retained rows to room_totals.

Case 1. Try the sample
Input
Art
Expected output
Rows: 2
Reported rooms: 2

Case 2. Another affected room
Input
Hall
Expected output
Rows: 2
Reported rooms: 2

Case 3. A room with one record
Input
Music
Expected output
Rows: 1
Reported rooms: 1

Stretch
Put the two retained rows into a short ticket with an exact command or Studio run instruction for another learner.

Reflect
Which two input rows are enough to show the issue, and why does the report count them separately?

Check your prediction
No. The remaining row gives one room total. The report treats the two spellings as distinct keys. One row produces one key.
