🐛
Coding🚀 Ages 7-10Beginner 6 min read

Debugging: Finding Mistakes

A primary-school coding lesson on debugging: learn what a bug is, why code goes wrong, and easy steps to find and fix mistakes, with examples and a quiz.

Key takeaways

  • A bug is a mistake in code that makes it work the wrong way
  • Debugging means finding and fixing those mistakes
  • Read your code step by step to spot the problem
  • Everyone gets bugs, even expert coders

What is a bug?

A bug is a mistake in code. It makes the program do the wrong thing, or stop working.

The computer is not being naughty. It is just doing exactly what the code says. If the code is wrong, the result is wrong too.

Finding and fixing bugs is called debugging. Every coder does it, every single day.

Why does code go wrong?

Code can go wrong for simple reasons:

  • A step is in the wrong order.
  • A word is spelled wrong.
  • A step is missing.
  • The wrong number is used.

Even one tiny mistake can stop a program. Computers are very fussy!

A bug you can spot

Look at this code. It should say a friendly hello:

print("Helo")

Can you see the bug? The word "Hello" is missing an l. The computer doesn't fix it for you. It prints exactly what you wrote: Helo.

To fix it, we correct the spelling:

print("Hello")

Now it prints Hello. Bug squashed!

A wrong-order bug

Order matters too. Look at these steps to greet someone:

print("Goodbye")
print("Hello")

This says goodbye before hello. That's backwards! We can debug it by swapping the lines:

print("Hello")
print("Goodbye")

Much better. This is the same idea you learned in Sequences: Putting Steps in Order.

How to debug, step by step

When your code goes wrong, try this plan:

  1. Read your code slowly, one line at a time.
  2. Pretend you are the computer. Do exactly what each line says.
  3. Find the line where things go wrong.
  4. Fix that one thing.
  5. Run it again to check.

This careful reading is like being a detective. You follow the clues until you find the problem.

Bugs are normal

Here is a secret: getting bugs does not mean you are bad at coding. Everyone gets them, even experts who write code for big games and apps.

The skill is not avoiding bugs. The skill is staying calm and fixing them. Each bug you fix makes you a stronger coder.

You can practice spotting mistakes in Getting Started with Scratch, where you can see your sprite move and quickly tell if a step is wrong.

Quick quiz

Test yourself and earn XP

What is a bug in coding?

What does debugging mean?

What is a good first step when code goes wrong?

How many steps does this print? ``` print("Hello") print("World") ```

FAQ

Long ago, a real moth got stuck inside a computer and caused a fault. The team taped it in their notebook and called the problem a bug. The name stuck!

Yes, all the time. Even the best programmers in the world spend a lot of their day debugging. It is a normal part of coding.