Variables in Scratch (User’s Name and Age)

A variable is a container of a value.

Consider the shoes you are wearing now. That shoes is a variable, and it’s value are your feet. If someone put on your shoes for a moment, then your shoes no longer contain your feet, but someone else, that is, your shoe-variable contains a different value. You see, your shoes is called a variable because its value may vary or may be different sometimes.

Also, a variable should have one specific value at any given time. You do not want your feet to share with someone else’ feet on the same shoes at the same moment, right?

The Answer block in Scratch is a variable. It’s value or data can change overtime depending on what the user typed in, and it can contain only one value at a time.

Here’s the problem

This Scratch project below has only one variable which is the Answer block. The character asks for two data: the user’s name and age. Yes, he can ask for the user’s name, put user’s answer in the Answer variable block, then display it. Then he can ask another data, say the user’s age, then put it in the Answer block again, and display the user’s age on screen.

The problem is that the character cannot display the user’s name again since the Answer block variable is now holding the user’s age. The user’s name is gone since a variable can only hold one data at a time.

The solution

The solution to this problem is to add another variable. Here’s how to do it.

Step 1: Go to the Variables Block section (it’s orange).

Step 2: Click the Make a Variable button to create your own variable (its white button)

Step 3: Give it a meaningful name, then click OK. Optionally, you can choose For all sprites or For this sprite only.

Step 4: Then as soon as you asked your user to enter a data, put that data into your variable. This means that you will have to use the Set variable to Answer block (it’s orange).

Step 5: Then you can now display the value of your variable by using it in the Say block instead of the Answer variable block.

Here’s an example

Notice that the character can now display the user’s name and age anytime he wants since both of these data have variables holding it.

Show us the Scratch project you made similar to this using variables. Paste the link to your Scratch project on the comment box below.

Leave a Reply

Your email address will not be published. Required fields are marked *