GoalBuild a program that counts with a variable and test it with a table of expected and actual results.
New words
test · to run a program and see whether it does what it shouldexpected result · what we think the program should do, worked out before we run itactual result · what the program really does when we run itdebug · to find a bug and fix it
Explanation
To build a counting program, follow four steps. First, make a variable and give it a clear name. Second, set it to 0 when the green flag is clicked. Third, put a ‘change’ block under the event that should be counted, such as a click on a sprite. Fourth, test the program. Before each test, work out the expected result. Then run the program and write down the actual result. If the two are different, there is a bug. The cause is often a wrong number, a ‘set’ block where a ‘change’ block should be, or a different variable chosen from the list. Showing the variable on the stage lets us watch its value.
Worked examples
Test
Expected rings
Actual rings
flag, no clicks
0
0
flag, then 5 clicks
5
5
5 clicks, then the flag again
0
0
A test table compares expected and actual results. The bell adds 1 to rings for each click. Both columns match, so these three tests pass.
Bug 1: the script uses ‘set’ and not ‘change’. Each click sets rings to 1 again. After 5 clicks the value is still 1, but the expected result was 5.
Bug 2: the script changes a different variable. After 5 clicks the variable rings shows 0, because the click script adds to clicks.
Class activity
Bug-hunt cards (pairs). Write six small programs on cards, each with one ‘set’ block, one ‘change’ block and a claim such as ‘after 3 clicks the value is 6’. Make some claims true and some false. Swap cards. For each card, work out the expected result by tracing it with a cup and counters, and say whether the claim is true. If it is false, find the bug. Who found the most bugs? If you have a computer or tablet, build the bell program and test it with a table.
Practice
1
The gong adds 2 to hits each time that it is clicked. What is the value of hits after 7 clicks? What is it after the flag is clicked again?
7 × 2 = 14. After the flag is clicked again, the ‘set’ block makes it 0.
2
Complete the test table for a script that adds 4 for each click.
A pupil expects 10 clicks to give 10 but sees 30. Which block has the wrong number?
The number in ‘change (rings) by (3)’. It should be 1. Each click added 3, and 10 × 3 = 30.
4
Name two things to check when the value does not change at all after clicking the sprite.
Check that the script starts with ‘when this sprite clicked’, that it is on the right sprite, that the ‘change’ block is under the hat block, and that it uses the right variable.