Page 187 - Learn To Program With Scratch
P. 187

granted and changes the laptop’s costume to the on image. Otherwise, the
                          script displays Access denied! and the sprite continues to show its initial off
                          costume.
                             The GetPassword procedure sets the gotPass flag to 0, to indicate that it
                          hasn’t received a valid password yet, and initializes the failCount variable (our
                          loop counter) to 0. It then executes a repeat loop with a maximum repeat
                          count of three. During each iteration of the loop, the user is prompted to
                          enter a password. If the user enters the correct password (Pass123 in this
                          example), the gotPass flag is set to 1, the procedure stops itself by invok-
                          ing the stop this script command, and execution returns to the caller.
                          Otherwise, if the user hasn’t used up all three attempts, an error message
                          is displayed, and the user is given another chance. If the user fails three
                          consecutive times, the repeat loop automatically terminates, and the pro-
                          cedure returns to the caller with the value of the gotPass flag still set to 0.



                                                    try it out 7-4
                            Open this application and run it . What happens if you enter paSS123 (instead
                            of Pass123) for the password? What does this tell you about string comparison
                            in Scratch? Try to implement the GetPassword procedure using a repeat until
                            block .



                          Counting by a Constant Amount

               CountingBy   Of course, you don’t always have to increase your counters by 1 each time
              ConstAmount   through a loop. The script in Figure 7-13 at u, for example, has a sprite
                     .sb2  count from 5 to 55 in increments of 5. The script at v causes the sprite
                          to count down from 99 to 0 in decrements of 11—in other words, 99, 88,
                          77, ... , 11, 0.

                                                 







                          Figure 7-13: You can increment and decrement counters by amounts
                          other than 1 .

                             To see this counting technique in a practical application, let’s say that
                          we want to find the sum of all even integers from 2 to 20 (inclusive). (That
                          is, we want the sum 2 + 4 + 6 + 8 + ... + 20.) The script of Figure 7-14 does
                          exactly that.






                                                                  Repetition: A Deeper Exploration of Loops   165

                                           www.it-ebooks.info
   182   183   184   185   186   187   188   189   190   191   192