Today you build a small Scratch program that adds up a team's scores, then you meet a broken copy of it. The broken one runs perfectly happily and gives the wrong answer, which is the hardest kind of bug to find.
You will trace it by hand, fix it at your device, and then read what an AI assistant said about the same code and decide whether to believe it.
Four minutes. The point of building it first is that they own the correct version, so the broken copy is something they can compare against rather than a puzzle from nowhere.
Go to scratch.mit.edu and click Create. Call the project Score Adder.
You will build on the cat sprite that is already there, so there is nothing to add.
Signed-out pupils can build and run but not save to the site. Have them save the class way.
In Variables (orange), click Make a List and call it scores. Then make two variables the same way: total and count.
Now build the start of the program. It clears the list, then adds four scores to it.
when green flag clicked
delete all of [scores v]
add (3) to [scores v]
add (5) to [scores v]
add (2) to [scores v]
add (6) to [scores v]
delete all of matters: without it the list grows every time they click the flag, and the totals climb mysteriously. Let anyone who omits it see that happen once.
Set total and count to 0, then use a repeat loop from Control (gold) to walk the list. Each time round it adds one score to the total and counts it.
Read it and predict what total and count will hold at the end, before you click the green flag.
when green flag clicked
delete all of [scores v]
add (3) to [scores v]
add (5) to [scores v]
add (2) to [scores v]
add (6) to [scores v]
set [total v] to (0)
set [count v] to (0)
repeat (length of [scores v])
change [total v] by (item (count) of [scores v])
change [count v] by (1)
end
Predict first, before anyone clicks the green flag. The intended answer is total 16 and count 4. This version actually starts reading at item 0, which Scratch treats as empty, so watch for pupils noticing the first score is skipped. Either outcome is a good conversation; the fixed ordering comes next.
The counter has to start at 1, because Scratch numbers list items from 1, and it must go up after the score is added. Change set count to 0 to set count to 1.
Click the green flag. The cat should say 16, the total of 3, 5, 2 and 6.
when green flag clicked
delete all of [scores v]
add (3) to [scores v]
add (5) to [scores v]
add (2) to [scores v]
add (6) to [scores v]
set [total v] to (0)
set [count v] to (1)
repeat (length of [scores v])
change [total v] by (item (count) of [scores v])
change [count v] by (1)
end
say (join [Total: ] (total)) for (3) seconds
This is the working version and they built it. Make sure everyone sees 16 before moving on, because the next step depends on them knowing what right looks like.
You're previewing this lesson. Get full access to this lesson and hundreds more — each one ready to teach, with interactive activities, printable resources and pupil progress tracking built in.