Back to the Introduction to Computer Science outline The Course Maker
Introduction to Computer Science outline
Week 6 · Coding Lab

Week 6 — Coding Lab / Programming Studio · "Counting, Ranges & Grids"

Introduction to Computer Science · CSCI 1101 Fall 2026 · Prof. Okafor Fictional sample

Course: Introduction to Computer Science — CS1 / Programming Fundamentals in Python (CSCI 1101) · Silver Oak University (fictional sample) · Prof. Okafor
Objective: Objective 4 — write a for/range accumulator and a nested-loop pattern; trace range() and nested-loop output; find & fix an off-by-one · SLO A (write & run) + SLO B (trace & debug)
Worth 50 points · Coding Labs group = 15% of the grade · Coding Lab 6
Format: a hands-on programming studio worked in a free online Python environment — you'll (a) write a counting loop and a grid, (b) trace range() calls and a nested loop (predict, then run), and (c) find and fix an off-by-one bug — and then catch the AI's mistake when it writes a loop or predicts a count.

This is the course's signature weekly component. Every instructional week has one Coding Lab. Everything runs in your browser — nothing to buy or install. The whole habit of this lab (and this course): don't guess what a range or loop produces — list it, run it, and count the output.


Part 1 — The Big Picture

This week you learned the loop you'll write most: for with range() for "do this a known number of times," plus nested loops for grids and tables. The one fact that bites everyone — and the #1 off-by-one source in programming — is that range(stop) stops before stop (the stop is exclusive). This lab makes that reflex: you'll write counting and grid loops, trace ranges and a nested loop (predict, then run), and debug an off-by-one. By the end you'll trust your loops because you ran them and counted the lines.

Your tools (both free, both in the browser):
- An online Python editor to write and run code: 🔗 https://www.online-python.com/ (or 🔗 https://www.programiz.com/python-programming/online-compiler)
- Python Tutor to watch code run step by step: 🔗 https://pythontutor.com/ — paste a program, click Visualize Execution, then step forward. (We'll use it on a nested loop.)


Part 2 — Setup (2 minutes)

  1. Open the online Python editor in a new tab.
  2. Delete any sample code so you have a blank editor.
  3. Type and run this to see the exclusive stop for yourself:
    python print(list(range(1, 5)))
    You should see [1, 2, 3, 4]four numbers, no 5. That's the rule the whole lab turns on. 🎯

Part 3 — (a) Write: A Counter and a Grid

Write each of these in the editor, run it, and paste your code and its output into your submission.

  1. Sum the numbers. Write a program that uses a for loop with range() to add up all the whole numbers from 1 to 8 and print the total. Let the loop compute it (don't type the answer), and put print outside the loop. Run it; write down what it printed.
  2. A grid of stars. Write a nested loop that prints a 4×4 grid of the # character — four rows, four # per row. (Hint: outer loop over rows, inner loop prints # with print("#", end=""), then a print() under the outer loop to end each row.)
  3. Count down. Write a for loop that prints the numbers 5, 4, 3, 2, 1 (in that order), each on its own line. (Hint: a negative step — and remember the stop is excluded.)

Part 4 — (b) Trace: Predict, Then Run

For each item below, first write your prediction in the table (don't run it yet!). Then run each one in the editor (use print(list(...)) for the range items) and fill in the "Actual" column. Where your prediction and the actual output differ, that's the lesson.

# Code Your prediction Actual (after running)
1 list(range(4)) ______ ______
2 list(range(1, 6)) ______ ______
3 list(range(0, 12, 3)) ______ ______
4 list(range(10, 5, -1)) ______ ______
5 total = 0 / for n in range(1, 6): total += n / print(total) ______ ______
6 for i in range(2): / for j in range(2): / print(i, j)and how many lines? ______ ______

Hint for #4: a down-count stops before the stop too — so where does it actually end?
Hint for #6: the inner loop runs all the way through for each step of the outer loop — count the lines.

Visualize one: paste item #6 (the nested print(i, j) loop) into Python Tutor and step through it. Watch the inner j loop run completely (both values) before the outer i advances — and watch the line count grow to 4.


Part 5 — (c) Find & Fix the Bug

Each program below is broken in the classic way for this week. For each one: run it, compare the output to what was wanted, then write (i) what's wrong, (ii) why, and (iii) the fixed program.

Bug A — "off by one." This is supposed to print the numbers 1 through 5, each on its own line, but it doesn't:

for i in range(1, 5):
    print(i)

Bug B — "the table is one short." This is supposed to add up 1 through 10 and print 55, but it prints the wrong number:

total = 0
for n in range(1, 10):
    total += n
print(total)

Part 6 — Analysis Questions

Answer in a sentence or two each:
1. In the trace table, which prediction did you get wrong (if any)? What did you learn from the gap?
2. Item #4 (range(10, 5, -1)) counts down and stops at 6, not 5. Why does it stop there?
3. In item #6, the loop printed 4 lines. Explain — in terms of the outer and inner loops — why it's 4 and not 2.
4. For Bug A and Bug B, both were the same kind of mistake. In your own words, what is the "exclusive stop" rule, and how do you write a range to include an endpoint n?
5. Connect it: these off-by-one bugs don't crash — the program runs and gives a wrong answer. Why does that make them more dangerous than a SyntaxError, and what's your defense against them?


Part 7 — AI-Critique Moment (required — this is the BYOAI step)

Now bring in your approved chatbot (Gemini, Claude, or ChatGPT) and be the programmer who checks its work.

  1. Paste this to the chatbot: "Write a Python for loop that prints the numbers 1 through 5, and show me exactly what it prints. Then tell me how many lines THIS prints: for i in range(3): / for j in range(3): / print(i, j)."
  2. Check every claim by running it yourself in the editor:
    - Did it write the "1 through 5" loop with range(1, 6) — or slip and write range(1, 5), which stops at 4? (Chatbots routinely write "1 to n" as range(1, n) and quietly drop the last number.)
    - Did it say the nested loop prints 9 lines — or miscount it? (The correct answer is 9, because the print is in the inner loop: 3 × 3. Some models say 3 or 6.)
  3. Write 2–3 sentences reporting what the AI got right and at least one thing you had to correct or verify carefully. If it happened to get everything right, say how you confirmed each one by running it (and listing the range) — that's the skill.

The habit all term: the tool drafts, you run it and judge. A chatbot will confidently include the stop value or miscount a nested loop — catching it by running the code is the entire point of this course.


Part 8 — What to Submit

Submit a single document (or text entry) with: your Part 3 programs and their outputs; your completed Part 4 trace table (both columns, including the line count for #6); your Part 5 bug answers (what's wrong, why, and the fix for each); your Part 6 answers; and your Part 7 AI-critique paragraph. Due Sunday, Oct 11, 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 actually running the code in Python. Students' Part 3 wording/characters vary; grade those on "does it run and produce the right shape/value."

Part 3 (model, run-verified):
1. Sum 1–8: total = 0 / for i in range(1, 9): total += i / print(total)36 (the stop is 9 so 8 is included). ✓
2. 4×4 grid:
python for r in range(4): for c in range(4): print("#", end="") print()

#### #### #### ####
✓ (4 lines; the print() is under the outer loop)
3. Count down 5→1: for i in range(5, 0, -1): print(i)5, 4, 3, 2, 1 on five lines. ✓ (stop 0 is excluded)

Part 4 trace table (run-verified):

# Code Actual output
1 list(range(4)) [0, 1, 2, 3]
2 list(range(1, 6)) [1, 2, 3, 4, 5]
3 list(range(0, 12, 3)) [0, 3, 6, 9]
4 list(range(10, 5, -1)) [10, 9, 8, 7, 6]
5 sum for n in range(1, 6) 15
6 nested print(i, j) 0 0 / 0 1 / 1 0 / 1 14 lines

Part 5 bugs (run-verified):
- Bug A for i in range(1, 5): print(i) → (i) it prints only 1 2 3 4 (on four lines), missing the 5; (ii) the stop value 5 is excluded (exclusive stop) — an off-by-one; (iii) fix: for i in range(1, 6): print(i) → prints 1 through 5.
- Bug B for n in range(1, 10): total += n → (i) it prints 45, not 55; (ii) range(1, 10) stops at 9 and misses the 10 — the same exclusive-stop off-by-one; (iii) fix: for n in range(1, 11): total += n → prints 55.

Part 6 (expected): (1) most commonly #4 (...6, not ...5) or #6 (4 lines, not 2). (2) range(10, 5, -1) counts down and stops before the stop 5, so the last value is 6. (3) The outer loop runs 2 times, and for each outer step the inner loop runs 2 times with a print inside it, so 2 × 2 = 4 lines. (4) The exclusive stop: range(start, stop) never includes stop — it goes up to stop - 1. To include an endpoint n, set the stop to n + 1 (e.g., range(1, n + 1)). (5) An off-by-one runs without error and returns a plausible-but-wrong answer, so nothing flags it — unlike a SyntaxError, which stops the program. Defense: run it, count the output / check the endpoints, and print(list(range(...))) when unsure.

Part 7 (AI-critique): full credit for a specific catch — most commonly the AI writing the "1 through 5" loop as range(1, 5) (stops at 4) instead of range(1, 6), or miscounting the 3×3 nested loop (the correct count is 9 lines). Full credit also if the student verified each AI claim by running it and listing the range.

Grading rubric — 50 points

Criterion Full Partial None
Part 3 — wrote & ran 3 programs (sum 1–8 = 36; a 4×4 grid; a 5→1 countdown; code + output shown) (14) 14 7–11 0–5
Part 4 — trace table (predictions attempted + all 6 actual outputs correct from running, incl. the #6 line count) (14) 14 7–11 0–5
Part 5 — found & fixed both bugs (what's wrong + why + fix for each; both are the exclusive-stop off-by-one) (12) 12 6–10 0–4
Part 6 — analysis (the down-count endpoint, the nested line count, the exclusive-stop rule + n + 1) (6) 6 3–5 0–2
Part 7 — AI-critique (names a specific thing checked/corrected by running) (4) 4 2 0–1

Quality gate (self-checked): every model output above (36; the 4×4 # grid; the 5 4 3 2 1 countdown; [0, 1, 2, 3], [1, 2, 3, 4, 5], [0, 3, 6, 9], [10, 9, 8, 7, 6], 15, the 4-line nested output; Bug A 1 2 3 4; Bug B 4555) was produced by actually running the code in Pythonexecution gate: PASS. No output is hand-traced. The lab grades the student's process (write → trace → debug), not a single fixed wording for the open-ended Part 3.

~ Prof. Okafor's edition · Fall 2026 · built with thecoursemaker.com