LA24AI

Courses / Learn from zero

Diagnose a small coding error with AI assistance

Outcome: A reproducible bug report and a tested-or-reasoned correction for a simple Python list operation.

Five connected lessons with worked examples and exercises

Teaching examples are not claimed live model runs; review each lesson’s limits.

Download practice worksheet ↓
Start at lesson one ↗
Lesson 1 / 5

Lesson 1: A symptom is not the cause

If a lamp won't turn on, the bulb isn't necessarily broken; the plug may be loose. “My program doesn't work” is similarly vague. Inspect a concrete example:

numbers = [2, 4, 6]
print(numbers[3])

Python list indexes begin at zero, so these three values occupy positions 0, 1, and 2. Index 3 is out of range. Try it: Write the index under each number. You can now explain the defect rather than merely quoting an error message.

Review question: Why does numbers[3] fail for a three-element Python list?

Python indexes from zero, so positions are 0, 1 and 2; position 3 does not exist.

Next lesson ↗
Lesson 2 / 5

Lesson 2: Give the assistant a reproducible description

A good request says: “In Python, I want the last item of [2,4,6]. The code uses numbers[3] and fails with an index error. What's the smallest correction, and why?”

For this list, numbers[2] or numbers[-1] yields the last item. The negative-index solution is convenient for nonempty lists of varying length, but it also fails when the list is empty.

Review question: Why include the intended result and error in an AI debugging request?

So the suggestion addresses the actual mismatch rather than inventing unrelated changes.

Next lesson ↗
Lesson 3 / 5

Lesson 3: Test the boundary case

Consider a three-item list, a one-item list, and an empty list. If empty lists are valid inputs, add a guard:

if numbers:
    print(numbers[-1])
else:
    print("The list is empty")

Try it: Write expected outcomes for each case. Avoid hiding every error with a broad exception handler; swallowing a symptom is not the same as fixing its cause.

Review question: What happens when numbers[-1] is used on an empty list?

It also raises an error; guard the empty case if it is a supported input.

Next lesson ↗
Lesson 4 / 5

Lesson 4: Demand an explanation of the change

Ask the assistant which line failed, why index 3 was invalid, what changed, and how to test the result. Compare “replace this line” with “there are only indexes 0–2, so use the last valid index or handle an empty list.” The second answer teaches a reusable idea.

Try it: Explain why the absence of an error is not sufficient evidence of a correct return value.

Review question: Why not silence the issue with a broad try/except?

It may hide symptoms without repairing the cause.

Next lesson ↗
Lesson 5 / 5

Lesson 5: Keep a reproducible fix record

Your report: symptom = index error; cause = nonexistent index; fix = last-item access for a nonempty list; extra test = empty list; policy = handle that case if the task requires it. Save before-and-after code and protect any secrets before sharing logs.

You learned: to separate observation, diagnosis, repair, and verification rather than repeatedly asking AI to rewrite a program blindly.

Review question: What belongs in a reusable debugging record?

Symptom, cause, minimal fix, ordinary test and edge-case test.

What did you accomplish?

Finish only when you can complete the final exercise and explain each step’s outcome or limits.

Topic-specific worked final project

Practice input

A program accesses a nonexistent index when printing the last item

Reference deliverable

Before: numbers=[2,4,6]; print(numbers[3]) raises IndexError.
After: print(numbers[-1]) returns 6 for a nonempty list.
For numbers=[], check if numbers first and print an empty-list message.

How do we judge success?

Tests: [2,4,6]→6, [9]→9, []→clear message; do not merely hide the original exception.