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 COMPUTER
A table for every box.
Read one line, then write what is inside the box. This is called a trace table.
| Line | Code | apples keeps |
|---|---|---|
| 1 | apples = 3 | 3 |
| 2 | apples = apples + 2 | 5 |
| 3 | apples = apples - 1 | 4 |
| 4 | print(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.
Be the computer
x = 2
x = x * 3
x = x + 1What does x keep at the end?
Trace a loop
total = 0
for n in [1, 2, 3]:
total = total + nWhat does total keep after the second turn?
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?
Talk to the duck
Why does that help?
The best bug report
Which message helps them most?
Check the box
def double(n):
return n + 2Which check catches the bug in double?
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.
10OUTPUT
Want a fresh start? Clear only this page.
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!