A bug is a mistake in a program. Debugging is finding it and fixing it. Programmers spend a lot of time doing this, and getting good at it is more useful than memorising any command. Here is a routine children can follow.
Why are they called bugs?
The word was used in engineering long before computers, and a story from the 1940s about a moth found inside an early computer helped make it famous. Today it simply means something isn't working as expected.
The five-step bug hunt
Step 1: stay calm and describe it
Say, in one sentence, what the program should do and what it does instead. "It should add 1 to the score, but the score stays at 0." A clear description often points at the answer.
Step 2: read the error message
If the computer shows an error, read it carefully, especially the last line and any line number. Many messages say exactly what's wrong. See common Python mistakes for examples.
Step 3: find where it goes wrong
Narrow down the location:
- Print messages. Add lines such as
print("got here")orprint(score)to see how far the program gets and what values it has. - Comment out. Temporarily switch off parts of the code to see whether the bug disappears.
- Test a small piece. Run just the part you think is broken on its own.
Step 4: think like the computer
Read the code one line at a time, as if you are the computer, doing exactly what it says. Say each step aloud, tracking the values on paper. Often the gap between what you meant and what you wrote shows up.
Step 5: fix one thing, then test
Change only one thing at a time, then run it again. If you change five things and it works, you won't know which one fixed it.
An example
total = 0
for i in range(5):
total = i
print(total)The goal is to add up 0 to 4 and get 10, but it prints 4. Describe it: "It should add each number, but it only shows the last one." Add print(total) inside the loop and watch: 0, 1, 2, 3, 4. The line total = i replaces the total each time. The fix is total = total + i.
Habits of good debuggers
- They stay patient and curious.
- They make small changes.
- They explain the problem aloud, even to a toy. This is called rubber duck debugging.
- They take breaks.
- They ask for help after trying, and explain what they've already tried.
Make it a game
Write a program with a deliberate bug and ask a friend or sibling to find it. The Tiny Coderz Puzzle Arcade has "spot the bug" challenges that train exactly this skill.
For parents
When your child is stuck, ask questions instead of fixing the code yourself: see how to help your child when they're stuck and the bug mindset.