0

Find the bug in code

  1. bug

    The robot missed

    The robot should land on the star. But it stopped in the wrong place. A mistake in code is called a bug. Every coder makes bugs. Finding them is part of coding.

  2. move right
    move up

    A wrong line

    The star is 2 squares right. Tap Run the program and watch. Line 2 goes up, not right. So line 2 is the bug.

  3. move_right()
    move_right()
    move_down()

    A missing line

    The robot must go right 3, then down 1. Count the rights in the code. There are only 2. One move_right() line is missing.

  4. repeat 5 times
    move right

    A wrong number

    Loops can have bugs too. The star is 4 squares right. This loop says repeat 5:. Run it and the robot goes 1 square too far. The number should be 4.

  5. repeat 3:
    move_right()

    A missing indent

    Look at this loop. move_right() is not pushed in. So it is not inside the loop. The robot would not know what to repeat. Add the indent to fix it.

For parents

Goal: Your child learns to find three kinds of bugs: a wrong line, a missing line and a wrong number.

  • When your child finds a bug, ask them to say its line number.
  • Tell your child that bugs are normal. Every coder makes them and fixes them.
  • In the labs your child watches buggy programs run, taps the wrong line, picks the fix and sorts lines with spelling bugs.

Away from the screen: Give your child spoken steps to walk to the kitchen, with one wrong step on purpose. Let them find the bug and tell you which step it was.

Coding · Debugging
Back to top