Week 14 — Coding Lab / Programming Studio · "Watch the Stack"
Course: Introduction to Computer Science — CS1 / Programming Fundamentals in Python (CSCI 1101) · Silver Oak University (fictional sample) · Prof. Okafor
Objective: Objective 8 — write & trace recursive functions and the call stack · SLO A (write & run) + SLO B (trace & debug)
Worth 50 points · Coding Labs group = 15% of the grade · Coding Lab 14
Format: a hands-on programming studio in a free online Python environment — you'll (a) write, (b) trace code and predict its output, and (c) find and fix a bug — then catch the AI's mistake.
Everything runs in your browser — nothing to install. The habit: run it and read what Python actually prints.
Part 1 — The Big Picture
This week a function calls itself. This lab makes it concrete and visible: you'll write recursive functions, step through the call stack in Python Tutor to watch calls pile up and unwind, and fix a recursion that never stops.
Your tools: 🔗 https://www.online-python.com/ (write & run) and 🔗 https://pythontutor.com/ (visualize).
Part 2 — (a) Write
Write each program, run it, and paste your code + output.
1. Recursive factorial. Write factorial(n) with a base case. Print factorial(5) and factorial(0).
2. Countdown. Write a recursive countdown(n) that prints n down to 1 then Go!. Run countdown(3).
3. Watch the stack. Paste your countdown into Python Tutor, run countdown(3), and step forward. Describe what happens to the call stack (how many frames are open at the deepest point, and the order they finish).
Part 3 — (b) Trace: Predict, Then Run
Predict each output first (don't run yet!), then run each and fill in "Actual."
| # | Program | Your prediction | Actual |
|---|---|---|---|
| 1 | factorial(5) (recursive) |
______ | ______ |
| 2 | factorial(0) (recursive) |
______ | ______ |
| 3 | countdown(3) output (4 lines) |
______ | ______ |
| 4 | sum_to(5) where sum_to adds n + sum_to(n-1), base 0 |
______ | ______ |
| 5 | a factorial with NO base case, e.g. def f(n): return n*f(n-1); run f(5) |
______ | ______ |
Hint for #5: what stops it? Run it (carefully) and read the last line of the error.
Part 4 — (c) Find & Fix the Bug
This recursion is supposed to count down and stop, but it never does. Run it (it raises an error), then write (i) the error type, (ii) why, and (iii) a fixed version.
def count_down(n):
print(n)
count_down(n - 1)
Part 5 — Analysis Questions
- In #3, what order did
countdownprint 3, 2, 1, and Go!? Why doesGo!come last? - In Python Tutor, how many
countdownframes were open at the deepest point ofcountdown(3)? When did each one finish? - Why did #5 (no base case) fail? What is the one thing every recursive function needs to avoid this?
sum_to(5)returned 15. Explain how the values "add up" as the call stack unwinds.- Connect it: recursion and iteration can solve the same problems. Give one problem where recursion feels natural and one where a simple loop is clearly better — and say why.
Part 6 — AI-Critique Moment (required — this is the BYOAI step)
Bring in your approved chatbot and check its work.
1. Ask it: "Write a recursive factorial function and tell me exactly what factorial(5) returns and how many times the function is called."
2. Run its code and step through Python Tutor and check: did it include a correct base case (or would it recurse forever)? Is factorial(5) really 120? Did it count the calls correctly (factorial(5) makes 6 calls: n=5,4,3,2,1,0)?
3. Write 2–3 sentences on what it got right and one thing you had to correct or verify by running it.
The habit: the tool drafts, you run it and judge. Catching the AI's slip — by running the code — is the point.
Part 7 — What to Submit
Your Part 2 programs + outputs; your completed Part 3 trace table; your Part 4 bug answers; your Part 5 answers; and your Part 6 AI-critique paragraph. Due Sunday Dec 6, 11:59 p.m.**** (50 points).
Instructor answer key & model outputs — REMOVE BEFORE PUBLISHING TO STUDENTS
Execution gate: PASS — every output below was produced by running the code.
Part 3 trace table (run-verified):
| # | Actual output |
|---|---|
| 1 | 120 |
| 2 | 1 |
| 3 | 3 / 2 / 1 / Go! (four lines) |
| 4 | 15 |
| 5 | RecursionError: maximum recursion depth exceeded |
Part 4 bug (run-verified): (i) RecursionError (maximum recursion depth exceeded); (ii) there is no base case, so count_down calls itself forever (n goes 5, 4, 3, ... past 0 into negatives); (iii) fix — add a base case before the recursive call:
def count_down(n):
if n == 0:
return
print(n)
count_down(n - 1)
(now count_down(3) prints 3, 2, 1 and stops — run-verified).
Part 5 analysis (expected): (1) 3, 2, 1, then Go! — the function prints n before recursing, so the base case (Go!) is reached last. (2) At the deepest point there are 4 frames open (countdown(3), (2), (1), (0)); they finish in reverse order (0 first, 3 last) as the stack unwinds. (3) #5 had no base case → infinite recursion → RecursionError; every recursive function needs a base case. (4) sum_to(0) returns 0, then 1+0, 2+1, 3+3, 4+6, 5+10 = 15 as each frame returns. (5) recursion feels natural for self-similar problems (folders inside folders, a tree); a loop is better for simple repetition (summing 1..100) — clearer and no stack overhead.
Grading rubric — 50 points
| Criterion | Full | Partial | None |
|---|---|---|---|
| Part 2 — wrote & ran factorial + countdown (with base cases; correct outputs) (14) | 14 | 7–11 | 0–5 |
| Part 3 — trace table (predictions attempted + all 5 actual outputs correct from running) (14) | 14 | 7–11 | 0–5 |
| Part 4 — found & fixed the no-base-case bug (error type + why + base-case fix) (12) | 12 | 6–10 | 0–4 |
| Part 5 — analysis (base-case-last order; call-stack frames; why #5 failed; unwinding sum) (6) | 6 | 3–5 | 0–2 |
| Part 6 — AI-critique (names a specific thing checked/corrected by running) (4) | 4 | 2 | 0–1 |
Quality gate (self-checked): every model output above was produced by running the code — execution gate: PASS.
~ Prof. Okafor's edition · Fall 2026 · built with thecoursemaker.com