image

WHEN YOU LEARN an interesting fact about birds or the name of a new friend, you store it in your memory. In the same way, when a computer keeps track of your score in a game or remembers your name, it’s using computer memory.

In your head, you remember facts and figures by giving them names. Without a name, any fact or figure is useless. For example, the number 15,000 is just a number. But with a name, it becomes an interesting thing to remember, for example:

Elephants can weigh as much as 15,000 pounds!

Computers remember things in the same way as you do. In programming languages, the name that’s given to something you want the computer to remember is called a variable.

In this adventure, you find out how to use variables in Scratch to remember and keep track of things.

Understanding Variable Blocks

Variables are like boxes into which you can put any combination of words, numbers, dates, or other values that you want to use or remember in your program.

If you want to write a program that asks a user to input the answers to several questions, you can use the ask () and wait block that you learned about in Adventure 4. After each question is asked, the answer is placed into the answer block.

Follow these steps to create a simple quiz program:

  1. Select File  ⇒  New from the top toolbar to create a new project.
  2. Drag the ask () and wait block from the Sensing Block Palette to the Scripts Area.

    Leave the question in the ask () and wait block set to “What’s your name?”.

  3. Drag a think () for () secs block from the Looks Block Palette and snap it to the bottom of the ask () and wait block.
  4. Drag an answer block from the Sensing Block Palette and snap it inside of the think () for () secs block.

    Your Scripts Area should now look like Figure 6-1.

image

Figure 6-1 Asking a question and thinking the answer

People say that the best way to remember people’s names when you first meet them is to say their name right away. This is exactly what Scratch the Cat is trying to do in this program.

Double-click the ask () and wait block to run the program. When Scratch the Cat asks for your name, type it in and press Enter on your keyboard, or click the checkbox on the Stage. Scratch the Cat “thinks” your name, as shown in Figure 6-2.

image

Figure 6-2 Scratch the Cat is thinking about your name

Let’s see what happens when Scratch the Cat asks another question. Follow these steps:

  1. Right-click the ask () and wait block in your Scripts Area and select Duplicate.

    A copy of the connected ask () and wait and think (answer) for () secs block is created.

  2. Connect the duplicated blocks to the original two blocks.

    Your Scripts Area should now look like Figure 6-3.

  3. Double-click the first ask () and wait block.
  4. Answer the first question with your own name.

    Scratch the Cat “thinks” about your name, just as before.

  5. Answer the second question with someone else’s name.

    Scratch the Cat “thinks” about that new name.

image

Figure 6-3 Asking and thinking about two names

Scratch the Cat is able to ask a question and then repeat your answer. But, as soon as she asks another question, she forgets the answer to the first one. That must be so embarrassing for her at parties!

We can solve this problem and help Scratch the Cat to be a better party host by using variables.

Variables Have Names

In this section, you create two variables to remember the two different answers to Scratch the Cat’s questions. Each variable in a program has a unique name that’s used to remember and recall its value.

You can name variables anything you want in Scratch. For this project, you create two variables and call them Name1 and Name2.

Follow these steps to create the two variables and use them to hold the two different answers:

  1. Click the Data category in the Block Palette.

    You see two buttons, Make a Variable and Make a List, as shown in Figure 6-4.

  2. Click Make a Variable.

    The New Variable pop-up window opens, as shown in Figure 6-5.

  3. Type Name1 into the text area to the right of Variable Name in the New Variable pop-up window.
  4. Leave everything else set as it is in the pop-up window and click OK.

    As soon as you click OK, a new block named Name1 appears in the Data Block Palette, and four new stack blocks become visible, as shown in Figure 6-6.

image

Figure 6-4 The Data Block Palette

image

Figure 6-5 The New Variable pop-up window

image

Figure 6-6 Your first variable, and the Data stack blocks

Variables Can Display on the Stage

Look at the Stage now. You see a new box in the upper-left corner with the name of your new variable and an orange box with a 0 in it, as shown in Figure 6-7. The 0 is the current value that’s stored in your variable. All variables start out with a 0 in them when they’re created.

image

Figure 6-7 Your first variable’s value on the Stage

When you first create a variable, it displays on the Stage. You can use a variable’s display for showing the current score in a game, for example, or just to help you visualize what’s happening in your program. You can drag the variable anywhere you want it to be on the Stage, or you can choose not to display it at all. To hide a variable, uncheck the checkbox next to it in the Data Block Palette, as shown in Figure 6-8.

image

Figure 6-8 Hide a variable by unchecking the box next to it

In a moment, you’ll use this Name1 variable to remember the first name that you tell Scratch the Cat. But first, use the following steps to create another variable that you’ll use to help Scratch the Cat remember the second name:

  1. Click the Make a Variable button in the Data Block Palette.

    The New Variable pop-up window opens.

  2. Give this new variable a name of Name2.
  3. Click OK to create the new variable.

    Your Data Blocks Palette should now look like Figure 6-9.

image

Figure 6-9 The Data Block Palette with two variables

Variables Can Change

The reason variables are named variable is because they can vary, meaning that you can change the value stored inside of them. The block that you use to change the value of a variable is the set () to () stack block, shown in Figure 6-10.

image

Figure 6-10 The set () to () block

In this section, you use the set () to () block and variables to help Scratch the Cat remember two names. Follow these steps:

  1. Drag a set () to () block from the Data Block Palette to the Scripts Area and snap it to the bottom of the first ask () and wait block.
  2. Change the value in the set () to () block’s drop-down menu to Name1.
  3. Drag an answer block from the Sensing Block Palette and snap it into the square inside the set () to () block.
  4. Drag the answer block out of the first think () block and remove it from the Scripts Area by dropping it back into the Blocks Palette.
  5. Drag the Name1 variable block from the Data Block Palette and snap it inside the first think () block.

    Your Scripts Area should now look like Figure 6-11.

  6. Drag another set () to () block from the Data Block Palette and snap it to the bottom of the second ask () and wait block.
  7. Change the value in this new set () to () block’s drop-down menu to Name2 (if it isn’t already set to that).
  8. Drag an answer block from the Sensing Block Palette and snap it inside the set () to () block.
  9. Remove the answer block from the second think () for () secs block and replace it with the Name2 variable block from the Data Block Palette.

    Your Scripts Area should now look like Figure 6-12.

  10. Drag a say () for () secs block from the Looks Block Palette and snap it to the bottom of your script.
  11. Drag a join () () block from the Operators Block Palette and snap it into the first space in the say () for () secs block, as shown in Figure 6-13.
  12. Drag the Name1 variable block from the Data Block Palette and drop it into the second rectangle inside the join () () block, as shown in Figure 6-14.
  13. Drag another say () for () secs block from the Looks Block Palette and snap it to the bottom of the first one.
  14. Drag a join () () block from the Operators Block Palette and snap it inside the say () for () secs block.
  15. Drag the Name2 block from the Data Block Palette and snap it inside the second space in the join () () block.

    Your Scripts Area should now look like Figure 6-15.

  16. Double-click the first block in your script to run the program.

    Scratch asks you for two names and then greets you using both names!

image

Figure 6-11 Storing the first answer in a variable

image

Figure 6-12 Storing two answers in two variables

image

Figure 6-13 Snapping a join () () block inside the say () for () secs block

image

Figure 6-14 Dropping a variable block into a join () () block

image

Figure 6-15 Remembering and saying two names

Scratch Variables Are Persistent

Try running this script a few more times, using different names each time. Notice that after you submit a new name, it replaces the name that was previously in each variable, as you can see by watching the variable boxes displayed on the Stage.

One interesting thing about variables in Scratch is that they’re persistent. What this means is that you can stop a program and start it again and the program’s variables will be the same as they were when you stopped the program.

You can even close the browser window or shut down your computer and return to your program later on to find that the variables will be exactly the same as they were when you left.

Scratch Variables Are Big

If variables are like boxes, Scratch has some very big boxes.

Try entering multiple names or words in response to each question Scratch the Cat asks you. It doesn’t matter what you type, the variable remembers it until you run the program again and change the variable’s value.

Figure 6-16 shows how you can enter quite a bit of text into the ask () and wait text box and Scratch stores it the same as when you enter just a single name.

image

Figure 6-16 Storing long values in a variable

Making Lists

You’ve seen how you can use variables to store single values in Scratch. But, what if you have a long list of names to remember? Creating all those variables to store names can be time consuming! And, what if you don’t know how many names you want Scratch the Cat to remember?

This is where lists come in! Lists in Scratch are just like lists in real life. You can add items to them, remove items from them, and change any item from one value to another.

This section shows you how to use lists with loops to make Scratch the Cat ask for and remember as many names as you’d like!

Making a List

Lists work just like variables, except each list can hold more than one value. In other programming languages, lists are called arrays.

Follow these steps to create your first list in Scratch.

  1. Select File  ⇒  New from the top toolbar.
  2. Click Make a List in the Data Block Palette.

    The New List pop-up window opens, as shown in Figure 6-17.

  3. Enter Names in the List name text box and click OK.

    The Names list appears in the Data Block Palette, along with the nine list blocks, as shown in Figure 6-18.

image

Figure 6-17 The New List pop-up window

image

Figure 6-18 The List blocks

Take a look at the Stage. The Names list is displayed in the top-left corner, as shown in Figure 6-19.

image

Figure 6-19 The Names list on the Stage

When a new list is created, it doesn’t have any values in it. A list with nothing in it is called an empty list. Follow these steps to add and remove a couple of items from your list.

  1. Click the small plus sign in the lower-left corner of the Names list on the Stage.

    A blank text area appears at the top of the list and the length of the list changes to 1, as shown in Figure 6-20.

  2. Enter a name into the blank text box in the Names list.

    If you press Enter (or Return) now, a new blank item is added to the list. If you click anywhere outside of the list, the value you entered is stored without creating a new, blank item.

  3. Add several more items to the list.
  4. Click inside each of the list items and click the x with a circle around it (see Figure 6-21) to delete each of them.
image

Figure 6-20 Adding an item to a list

image

Figure 6-21 Deleting items from a list in the Stage

Most of the time when you use lists, you add and remove items from them using Scripts that you create. In the next section, you find out how to use the List stack blocks to make Scratch the Cat remember a bunch of names!

The Ultimate Party Host

In this project, you create a program that can be very helpful at parties, in school, and just about anywhere where a lot of people gather. The ultimate party host can remember any number of peoples’ names, in order, along with each person’s age.

Introducing the Lists and Variables

The first thing you do is create the arrays and a variable that the program needs.

  1. Click File  ⇒  New to create a new project.
  2. In the Data Block Palette, create two new lists: Names and Ages.
  3. Create a new variable called Name Number.
  4. Your Data Block Palette should now look like Figure 6-22.
  5. Drag a when green flag clicked block from the Events Block Palette to the Scripts Area.
image

Figure 6-22 The Party Host’s variable and lists

Asking for Names and Ages

Next, you use a question and a loop to gather information about each party guest. Follow these steps:

  1. Drag the ask () and wait block from the Sensing Block Palette and change its question to How many people are there?
  2. Drag a repeat () block from the Control Block Palette and snap it to the bottom of the ask () and wait block.
  3. Drag an answer block from the Sensing Block Palette and drop it inside the oval space in the repeat () block.
  4. Drag an ask () and wait block from the Sensing Block Palette and snap it inside the repeat block.

    Leave the question in the ask () and wait block set to “What’s your name?”

  5. Drag an add () to () block from the Data Block Palette and snap it to the bottom of the ask () and wait block inside the repeat () block.
  6. Drag an answer block from the Sensing Block Palette and snap it inside the add () to () block.
  7. Using the drop-down menu, change the second value in the add (answer) to () block to Names.

    Your Scripts Area should now look like Figure 6-23.

  8. Drag an ask () and wait block from the Sensing Block Palette to the Scripts Area and snap it to the add (answer) to (Names) block inside the repeat () block.
  9. Change the question in this block to How old are you?
  10. Drag an add () to () block from the Data Block Palette to the Scripts Area and snap it to the bottom of the ask (How old are you?) and wait block.
  11. Drag an answer block from the Sensing Block Palette and place it inside the add () to () block.
  12. Change the drop-down menu in the add (answer) to () block to Ages.
image

Figure 6-23 Gathering names and adding them to a list

Your Scripts Area should now look like Figure 6-24.

image

Figure 6-24 Adding to the Names and Ages lists

Click the Green Flag to try out your program! Enter a number in response to the first question. Keep the number pretty low for now (two or three) so that it doesn’t take too long to answer all the questions.

As you answer the questions, items are added to the two lists on the Stage. In the next part, you write code to make Scratch remember and repeat the names and ages of each person.

Recalling Names and Ages

To remember and use items in a list after you’ve created it, you put the number of a list item into the item () of () block. If you want to make a sprite say what the third item in the Names list is, for example, you’d use the block shown in Figure 6-25.

image

Figure 6-25 Saying the third item in the Names list

In this adventure, the program you create first tells you how many items are in the list of partygoers and then it says the names and ages of each guest. To do this, you use a loop to repeat the say () for () secs block for each item in the list. Here’s how you do it:

  1. Drag a say () for () secs block and snap it to the bottom of the repeat () block.
  2. Drag a join () () block from the Operators Block Palette and snap it into the first space in the say () for () secs block.
  3. Drag another join () () block from the Operators Block Palette and snap it into the first space in the join () () block you just placed.
  4. Change the first value in this new join () () block to I know.
  5. Drag a length of () block from the Data Block Palette and snap it inside of the second space in this join () () block.
  6. Change the value in the length of (Names) block to Names.
  7. Change the value in the second space in the outer join () () block to people.

    When this whole block is done, it should look like Figure 6-26.

  8. Drag a set () to () block from the Data Block Palette and snap it to the bottom of the say () for () secs block.
  9. Change the first value of the set () to () block to Name Number and change the second value to 0.
  10. Drag a repeat () block from the Control Block Palette and snap it to the end of the script.
  11. Drag a length of () block from the Data Block Palette and drop it inside the oval in the repeat () block.
  12. Change the value in the length of () block to Names.

    At this point, the repeat () loop runs one time for each item in the list called Names.

  13. Drag a change () to () block from the Data Block Palette to inside of the repeat (length of (Names)) block.
  14. Change the first value in the change () to () block to Name Number and the second value to 1.
  15. Drag a say () for () secs block to the Scripts Area and snap it inside the repeat (length of (Names)) block, after the change (Name Number by (1)) block.
  16. Drag a join () () block and snap it inside of the first space in the say () for () secs block.
  17. Drag an item () of () block from the Data Block Palette and snap it into the first value in the join () () block.
  18. Drag the Name Number variable block into the first value of the item () of () block and set the second value to Names.
  19. Drag an item () of () block into the second value in the join () () block.
  20. Drag a Name Number variable block into the first value of this new item () of () block and set the second value to Names.
  21. Drag another join () () block into the second open value of the first join () () block. In the first open space, type the word is with spaces before and after it.
  22. Drag an item () of () block from the Data Block Palette into the second value in the join () () block.
  23. Drag a Name Number variable block into the first value of this new item () of () block and set the second value to Ages.

    You may need to take apart blocks and put them back together in order to get it just right. Compare your finished say () for () secs block to the finished version in Figure 6-27.

  24. Drag a delete () of () block from the Data Block Palette to the bottom of the when green flag clicked block.
  25. Change the first value of the delete () of () block to all, and change the second value to Ages.
  26. Drag another delete () of () block from the Data Block Palette and snap it to the bottom of the first delete () of () block.
  27. Change the first value of this delete () of () block to all, and change the second value to Names.
image

Figure 6-26 The finished people counting block

image

Figure 6-27 The finished say () for () block to remember names and ages

When your program is finished, it should look like Figure 6-28.

image

Figure 6-28 The finished Party Host Scratch program

How did you do? Click the Green Flag to run the program. If it doesn’t work correctly, check all your blocks carefully one at a time until you find the error.

Further Adventures in Coding

Visit Mr. B’s code academy to watch a video explaining variables at www.youtube.com/watch?v=_sVtcPgHAjI.

image

Achievement Unlocked: Creating memories.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.47.253