⭐ Guided activity: Using a created variable
Let’s create variables num1 and num2.
Figure 152: Creating the variables num1 and num2
Let’s set variables num1 and num2 to be random numbers between 0 and 10 every time the flag button is clicked.
To do so, we need to follow these steps:
Step 1: From Events, pick up the block “when flag is clicked”
Step 2: From Variables, pick up the block “set variable to 0”
Step 3: From Operators, pick the block “pick random from 1 to 10” and let it replace the value 0 in the block created in step 2 as shown in Figure 152.
Figure 153: Picking a random number
Step 4: Duplicate the block “set num1 to ‘pick random from to 10’” created on the step 3.
Figure 154: Duplicating a block
Step 5: In the block created up to the step 4, replace num1 by num2.
Step 6: On the stage the output of num1 variable is overlapped with the output of num2 variable. Now drag and drop the output of num2 variable from above the output of num1 variable. That is illustrated in the figure below.
Figure 155: Num1 and num2 are given a random value
We can see in our code that we have created two variables: “num1”, and “num2”. Both variables have been set to pick a random number between 1 and 10. Therefore, when the green flag is clicked, each of both variables will be given a random number between 1 and 10 to store!
Next, we need Scratch Cat to tell us what these two numbers are. We can do this using the following code (Figure 156).
Figure 156: Cat saying the two random numbers
When a green flag is clicked, the cat will display the two random numbers. From what the cat displays, you can confuse the numbers and think that it is 93 instead of 9 and 31. With Scratch we will be able to correct that error.
To correct that, we will create some more code. We are going to join the string “Var1:” with its value. After that, we will have to join “Var2:” with its value as well. The cat is going to combine both values and display it at once.
Figure 157: Joining Var1 and Var2 with its values
The output of the above program is shown above.
Figure 158: Output after joining Var1 and Var2
We show the values of both variables and then display them as Var1:2 Var2:10. It does not put some space in between but at least we can see some difference.
From the above examples, we have been using the global variable. We have stored some values and conditions to var 1 and var 2. We can use the values over and over. However, there is another option that we can use without storing this into variables.
Figure 159: Joining Var1 and Va2 without storing their values
This is a value returned by the function “pick random” and can only be used once. This means that you will have to build new codes if you need to use the same values. These two pieces of code produce the same result — but in the second example, the random numbers that are generated are not saved. So, yes, they exist in that one instance of the program, but if you want to use these same numbers at some other point in the program, you can’t — because you didn’t save their information in a variable!
So, by using the second example, it would be difficult if for example, we were to extend the program by getting Scratch Cat to compare the numbers and see if they are the same instead of stating their values. What if we want Scratch Cat to report it when we find a match? What if we want to keep track of the number of times the two random numbers match? It is obvious that we need variables to store values in Scratch to achieve these!