โ†Previous page

08BUGS ยท DETECTIVE TOOLS

A detective's toolbox.

Real programmers have tricks to catch the sneakiest bugs. Here are four you can use every day.

THE TOOLBOX

Four tools for bug hunters.

Be the computerFollow the code line by line and write down what each box keeps.
Spy with printAdd a print to peek inside a box while the code runs. Remove it when you are done.
Explain it out loudTell a friend, a toy, or a rubber duck what each line does. Bugs pop out when you say them.
Write a bug reportSay what you did, what you expected, and what happened instead.

BE THE COMPUTER

A table for every box.

Read one line, then write what is inside the box. This is called a trace table.

LineCodeapples keeps
1apples = 33
2apples = apples + 25
3apples = apples - 14
4print(apples)shows 4

Detective tip: If you wanted 5, the table shows the exact line where things went wrong: line 3.

YOUR TURN

Use your tools.

Try each tiny question. A wrong answer becomes something to review.

0Completed
7Not tried
0Review
01

Be the computer

x = 2 x = x * 3 x = x + 1

What does x keep at the end?

02

Trace a loop

total = 0 for n in [1, 2, 3]: total = total + n

What does total keep after the second turn?

03

Where to spy?

The total is wrong at the end. PyBot wants to see it change.

total = 0 for n in [4, 9, 2]: total = n print(total)

Where does a spy print(total) help most?

04

Talk to the duck

PyBot explains its code, line by line, to a rubber duck.

Why does that help?

05

The best bug report

A friend's game has a bug. You want to tell them.

Which message helps them most?

06

Check the box

def double(n): return n + 2

Which check catches the bug in double?

07

Spy, find, fix

The total should be 10. Add print(total) inside the loop to spy. Find the extra line and delete it. Then delete your spy print too.

Goal output
10
OUTPUT

Your work is saved here.

Want a fresh start? Clear only this page.

!
BIG IDEA

Good detectives look, they don't guess.

Trace the boxes, spy with print, explain it out loud, and report clearly. Now you are a real bug hunter!