The_Official_Raspberry_Pi_-_Beginner’s_Book_Vol1,_2018 (1)

(singke) #1

(^66) THE OFFICIAL RASPBERRY PI BEGINNER'S GUIDE
You can do more with a variable than modify it. Click and drag the say loops for 2 secs
block to break it out of the repeat 10 block and drop it below the repeat 10 block. Click and
drag the repeat 10 block to the blocks palette to delete it, then replace it with a repeat until
block, making sure the block is connected to the bottom of the say loops for 2 secs block
and surrounds both of the other blocks in your sequence. Click on the Operators category in the
blocks palette, colour-coded green, then click and drag the diamond-shaped n = n block and
drop it on the matching diamond-shaped hole in the repeat until block.
when clicked
set loops to^0
repeat until =
say loops for^2 secs
change loops by^1
This Operators block lets you compare two values, including variables. Click on the Data
category in the blocks palette, drag the loops reporter block into the first empty square in the
n = n Operators block, then click on the second empty square and type the number ‘10’.
when clicked
set loops to^0
repeat until loops =^10
say loops for^2 secs
change loops by^1
Click on the green flag above the stage area, and you’ll find the program works the same
way as before: the cat sprite counts from 0 up to 9 (Figure 4-8) and then the program stops.

Free download pdf