Materials & Prep
Prepare the code listing below in the class editor or on a one-page handout. Students need access to a Python editor or notebook. Write the trace organizer on the board: inputs, outer condition, inner condition, returned value, expected value.
Opening: Commit to a Prediction
Display
A store policy says nonmembers who spend at least $100 receive a 5% discount. Without running the code, what does this call return?
```python
def discount(member, total)
if member
if total >= 100
return 20
else
return 10
else
if total > 100
return 5
else
return 0
print(discount(False, 100)) ```
Have students write one prediction, 0 or 5, and underline the condition they think decides it. Ask them to recall: when Python reaches return, does it continue checking the other branches? Do not confirm the answer yet. Tell students that the goal is to trace the actual path, compare it with the policy, and repair the code.
Direct Instruction: Model One Trace
Think aloudSet the inputs first: member is False and total is 100. The outer condition, member, is False, so Python skips the entire indented member branch and moves to else. It does not run both branches.
Worked example
```text Inputs: member = False, total = 100 Outer condition: member Result: False, so take the outer else branch Inner condition: total > 100 Comparison: 100 > 100 is False Returned value: 0 ```
ExplainThe code actually returns 0, but the stated policy requires 5 because 100 is at least 100. The faulty decision is the greater-than sign in the nonmember branch. The corrected condition is total >= 100. When return runs, the function call is finished, so no later branch runs.
ConfrontAsk students to point to every line that ran for discount(False, 100) and cross out the lines that did not. Emphasize that indentation defines which condition belongs inside which branch; reading every line top to bottom is not the same as executing every line.
Guided Practice: Trace and Test Cases
Display
```python
def discount(member, total)
if member
if total >= 100
return 20
else
return 10
else
if total > 100
return 5
else
return 0 ```
Have students complete the trace organizer for these calls before running them:
```python discount(True, 120) discount(True, 80) discount(False, 120) discount(False, 100) discount(False, 80) ```
ScaffoldFor students who need support, provide the sentence frame: “The outer condition is , so I take the branch. The inner comparison is , which is . The function returns .” Have them mark only one path with arrows and cross out the unused path.
Check for understandingStudents show a hand signal for the predicted output of discount(False, 100), then explain to a partner why 100 > 100 is False. Ask one student to identify the exact character that must change. Listen for “change > to >=,” not “change the return value.”
Run the calls and compare actual output with the policy. Students should identify discount(False, 100) as the only incorrect case in this test set.
Independent Debugging: Repair the Artifact
Students copy the function into the editor, change the faulty comparison, and run all five test calls. Their working artifact must include the corrected function and printed results for every test.
Expected
```python
def discount(member, total)
if member
if total >= 100
return 20
else
return 10
else
if total >= 100
return 5
else
return 0 ```
Expected outputs, in the order listed, are 20, 10, 5, 5, and 0. Students must add one edge-case test of their own that checks a value just below, exactly at, or just above 100, and write one sentence explaining which branch the test checks.
CirculateAsk, “What input reaches the faulty branch?” and “What evidence shows your fix works?” If a student changes a return value instead of the comparison, have them test both 100 and a value above 100 so the cause of the error becomes visible.
ExtensionStudents rewrite the function using a different correct structure, such as an early-return or combined-condition version, then test it against the same five cases. They must explain why their version produces the same outputs.
Closing Check: Trace, Diagnose, Fix
Exit ticketWithout running the code, answer these three prompts.
- For discount(False, 100), which outer and inner branches execute, and what value does the original code return?
- What exact comparison causes the wrong result, and why?
- Write the corrected line and state the corrected result for discount(False, 100).
Collect the response. A complete answer identifies the outer else branch, the inner else branch, the comparison 100 > 100 as False, the original return value 0, the repair total >= 100, and the corrected result 5.