GoalBuild a script with an if block from a plan, test it against a prediction and find common bugs.
New words
build · to put blocks together to make a scripttest · to run a program and compare what it does with what we predictedbug · a mistake in an algorithm or a programdebug · to find a bug and fix it
Explanation
To build a script, first select the right sprite. Add the hat block that matches the input, and the blocks of the action under it. Then add an if block, put the condition in its pointed slot and put the conclusion blocks inside it. Next, test the program. Predict the result first, run it and compare. If the two are different, there is a bug. Look for the wrong sprite, a wrong number, a wrong dropdown choice or blocks in the wrong place.
select the sprite→add the hat block→add the action→add if and its condition→add the conclusion→test and debug
Worked examples
Bug 1: the say block is under the if block and not inside it. It is no longer part of the conclusion, so the Moth says ‘Warm!’ after every key press, even when it is not touching the Lamp.
Bug 2: the if block is outside the forever block. The condition is checked once, right at the start, before the Heron has moved. The Heron never says ‘Dinner!’ when it reaches the Fish.
Test
Expected
Actual
Match?
flag, no key pressed
Moth stays still
Moth stays still
yes
space pressed once, far from the Lamp
no speech
says Warm!
no
space pressed 4 times, touching the Lamp
says Warm!
says Warm!
yes
A test table helps. Each row has an expected result, worked out first, and an actual result, seen when the program runs. A row that does not match shows a bug.
Class activity
Human computer (3 players). The programmer writes a short algorithm on a card, with one if step, for example ‘Start when a button is tapped. Move the counter 2 squares. If the counter is on the star, then clap.’ The computer follows each step exactly, one at a time, moving a counter on a drawn track. The tester writes the expected result before the computer starts, and compares it with the actual result. Then the programmer secretly swaps two steps on the card to make a bug. Can the tester find which step is wrong?
Practice
1
A sprite touches the Fish but never says ‘Dinner!’. Give three places to look for the bug.
Check that the condition names the right sprite, that the if block is inside the forever block, that the say block is inside the if block, and that the script is on the right sprite.
2
The Kite should say ‘Wheee!’ when it touches the Cloud, but it never does. Find the bug and fix it.
The dropdown in the condition is set to Sun, not Cloud, so the program checks the wrong sprite. Change the dropdown to Cloud.
3
The Beetle starts at x: -75 and moves 25 steps for each key press. The Log is at x: 0 (they touch only on the same spot). Complete the test table. On which press does the Beetle say ‘Found it!’?
Press
1
2
3
4
Beetle’s x
?
?
?
?
Says Found it!?
?
?
?
?
Press 1: -50. Press 2: -25. Press 3: 0, so it says ‘Found it!’. Press 4: 25, past the Log, so it says nothing. It speaks only on press 3.
4
Kai wants the Cat to meow when the space key is pressed. His script starts with ‘when this sprite clicked’. What goes wrong, and how can he fix it?
The script starts when the cat is clicked, not when the key is pressed, so pressing space does nothing. He should start with ‘when space key pressed’.