Find the Bug
Your program will go wrong. Everybody's does, including the programs written by grown-up engineers. A mistake in code has a name: a bug. Hunting bugs is a skill, and it is mostly about looking carefully rather than guessing.
π‘ The idea
A bug is a mistake in the code, and finding it is a normal part of coding.
- The computer always does what you wrote, not what you meant.
- Read your program one line at a time and say out loud what each line does.
- Check the numbers first. A wrong turn is the commonest bug of all.
- Add wait 5 inside a loop to slow the drawing down and watch it happen.
- Change one thing, run it, and look. Never change five things at once.
π Words to know
- bug
- a mistake in a program
- debug
- find a bug and fix it
- test
- run the program to see whether it is right
π Watch this
This was meant to be a square, but it never closes. That gap is the bug.
repeat 4 [ forward 100 right 80 ]
βοΈ Your turn
- Run it and see the gap where the shape should close.
- Work out 360 divided by 4, then fix the turn.
- Now break it on purpose: change forward 100 to forward 50 on one side only.
- Try a different bug: put penup before the repeat and run it. Where did the drawing go?
- Write down, in your own words, what each bug did.
π― Challenge: Fix the square so that it closes, then use the same idea to draw a closed six-sided shape.
Show me one answer
repeat 4 [ forward 100 right 90 ] penup goto -150 0 pendown repeat 6 [ forward 60 right 60 ]
Your program