Index Ai Academy
Phase 0 · ToolsPython core

Finding bugs when nothing crashes

About 30 minLooking ahead: some prerequisites aren't done yet

Why this matters

An error message at least tells you where to look. The harder bugs give no message at all: the program runs, and the answer is wrong. AI code is full of these. A score is off by one, a filter lets the wrong rows through, a prompt is built from the wrong field. A model will happily write such code for you, so the skill that counts is noticing the answer is wrong, and finding out why.

  • Describe a bug as what you expected against what actually happened
  • Make a bug happen every time, including one that depends on random numbers
  • Look inside a running program with labeled prints and the step-through view
  • Check the usual suspects: off-by-one ranges, > against >=, and lines in the wrong block

When the answer is just wrong

A bug is any way a program does something other than what you meant. Some bugs stop the program with an error, and the errors lesson showed you how to read those. This lesson is about the quiet kind: no error, no warning, just a wrong answer.

Here's one. It should give the average of a week of order totals:

orders = [120, 80, 95, 140, 60, 110, 75]

total = 0
for amount in orders:
    total = total + amount
average = total / 6

print("average order:", round(average, 2))

It prints a number, and the number looks believable. That's what makes quiet bugs dangerous. The week has 7 days, but the code divides by 6, so the average comes out too high.

Debugging is a method, not a talent. The method has four steps, and the rest of this lesson takes them one at a time:

  1. Say exactly what you expected, and what happened instead.
  2. Make the bug happen every time.
  3. Look at what the program really does, one line at a time.
  4. Change one thing, then check again.

Step 1: expected against actual

"It doesn't work" is too vague to fix. "I expected 97.14 and got 113.33" is a lead. To get the expected value, work out a small case by hand, or with a calculator. Seven orders adding up to 680 should average 680 ÷ 7, about 97.14.

Once you know both numbers, the size of the gap is a clue. 113.33 is exactly 680 ÷ 6, which points straight at the division.

Tip: test with tiny, easy inputs first. With orders of 10, 20 and 30, you know the average is 20 without a calculator, so a wrong answer jumps out.