GoalShorten a long drawing program with a repeat block and find bugs in a shape loop.
New words
bug · a mistake in a programdebug · to find and fix the mistakes in a programtest · to run a program and see if it does the right job
Explanation
A long drawing program often has the same two blocks, move and turn, written again and again. We can shorten it with a repeat block. Count how many times the pair is written and put that number in the repeat block. The short program must draw exactly the same picture. If a shape comes out wrong, there is a bug. To debug, check three things. Is the repeat number the same as the number of sides? Is the turn angle 360 divided by the number of sides? Is the pen down before the loop starts? Fix one bug at a time and test the program again each time.
Bug
What you see
Fix
repeat number too small
the shape is open
use the number of sides
wrong turn angle
the wrong shape, or lines drawn over each other
use 360 ÷ sides
pen down missing or too late
nothing is drawn
put pen down before the loop
Worked examples
This long script draws a shape with 4 equal sides by writing the move and the turn four times. The short script does the same job with a repeat block. The long one has 8 blocks for the shape and the short one has 3, so 5 blocks are saved.
Here the sprite should draw a pentagon, which needs 5 sides and a turn of 72 degrees. The repeat number is 4, so the last side is missing and the shape stays open. The fix is repeat (5).
This script should draw a hexagon, but the turn is 90 instead of 60. The sprite turns 6 × 90 = 540 degrees, which is more than one full turn. It draws a four-sided shape and then goes over two of the sides again.
Class activity
Bug hunt (with a friend): each of you writes the algorithm for a regular shape on paper, such as “repeat 5: move 4 cm, turn right 72 degrees”, but hides one bug, for example a repeat number that is too small. Swap papers. Draw the shape with a ruler, one step at a time, turning by eye (or with a protractor if you have one). Does it close? Find the bug and write the fixed version.
Practice
1
The steps “move (30) steps” and “turn ↻ (120) degrees” are written three times. Write a shorter version.
This script should draw a hexagon but stays open. Which block is the bug and what is the fix?
The repeat number is 5. It should be 6, because a hexagon has 6 sides.
3
This script draws nothing. Why, and how do you fix it?
The pen goes down after the loop, when the drawing is over. Move pen down before the repeat block.
4
A hexagon drawn without a loop needs 12 blocks, six moves and six turns. How many blocks does the version with a repeat block need, and how many are saved?