The_Official_Raspberry_Pi_-_Beginner’s_Book_Vol1,_2018 (1)

(singke) #1
Chapter 4 Programming with Scratch^65

THE OFFICIAL RASPBERRY PI BEGINNER’S GUIDE


when clicked


set loops to^0


repeat^10


say loops for^2 secs


change loops by^1


Click the green flag again. This time, you’ll see the cat count upwards from 0 to 9. This
works because your program is now changing, or modifying, the variable itself: every time the
loop runs, the program adds one to the value in the ‘loops’ variable (Figure 4-7).


5 Figure 4-7: Thanks to the loop, the cat now counts upwards


COUNTING FROM ZERO
Although the loop you’ve created runs ten times, the cat
sprite only counts up to nine. This is because we’re starting
with a value of zero for our variable. Including zero and
nine, there are ten numbers between zero and nine – so the
program stops before the cat ever says ‘10’. To change this
you could set the variable’s initial value to 1 instead of 0.
Free download pdf