6 Variables
6 Variables
Variables
Overview
Objectives
2. Create a variable and use the variable in your programs.
Teaching 1. A box of nuts and lots of variables paper strips (step 3- Lead-in game)
Preparation
2. A whiteboard and a whiteboard marker (or you can use a blackboard and chalks)
3. One Codey Rocky and a Bluetooth dongle (or a USB cable) per student but it’s fine if 2 or
4. A computer per student, but it’s fine if 2 or 3 students share a computer. And install mBlock
5 on each computer;
Time 120-240min
Teaching Procedure
Step 1 Review——Functions
Review
How were functions used in a program in the previous lesson? (walking squarely and
dancing)
How did functions help make programs more efficient? ( A function can be called multiple
What will the programs be like if we don’t use functions? (The programs will be lengthy
because some pieces of code might be repeated multiple times. And the logic of the programs might
be unclear.)
What’s the difference between a function and a loop? (A loop is repeated in one certain
place in a program while a function can be called over and over again in different places in a
program.)
1
-
A function refers to a group of coding instructions that can be called over and over again. In
the mBlock 5, we need to make a block under the category of My Blocks. Then, we can call the
Imagine that a variable is a box. You can put a value into the box and replace it with another
value anytime. A scoreboard works in the same way, serving as a container that stores the match
scores. Before the match ends, you can change the scores on the scoreboard when necessary.
Specifically speaking, a variable is a storage location in a computer, and it is paired with a symbolic
name. When a variable is created, the computer will leave a memory location to store the variable
and give the location a symbolic name. Through the variable name, programs can read and modify
It’s important to give a variable a proper and simple name because this helps to ensure the
variable can work properly. For instance, if we name the scoreboard as “Red Team”, then the
scoreboard will represent the scores that the red team wins in the match. When the red team has a
three-point shot, the number on the scoreboard will change accordingly. A new value takes the place
The first one is Assignment. You can copy a value into a variable. For example, a variable
“Red Team” is created to store the scores of the red team, and the initial value of the variable is set
to be 0. The second one is Revising Information, which means you can change the value stored in
the variable by adding, subtracting, multiplying or dividing them. For instance, if the red team makes
a successful shot, then the value in the variable “Red Team” will increase by 2. The third one is
Comparison. You can read the current value and compare it with another value. Through
comparison, you can tell that when a variable is greater than a certain value, then something will
happen. For instance, if the scores of the red team exceed the scores of the blue team, then the red
team wins.
There are plenty of examples of variables in real life. The Health of characters in games is an
example. Similarly, the health points of characters are changing over time in games. The amount of
2
-
money in your wallet and the number of candies are both examples of variables. We can read the
values and change the amounts of money and candies by adding or subtracting them. Moreover, we
can compare the values with other values. For instance, if the amount of money is greater than the
price of a certain product, then we can afford to purchase the product; if the amount of candies in
the jar exceeds a certain number, then we can share them with friends.
A little squirrel has a nuts box. Every day, it puts nuts into the box and takes nuts out of the
It’s another day. In the morning, the squirrel gets up and opens its nuts box, finding out there
Teaching Preparation Teachers prepare a box that’s shadow. Put a white paper on the box
and writes down a number 10 on the paper. Open the box and show the number to students.
Teaching Preparation Teachers prepare some paper strips. Each paper strip describes a
situation of how the number of nuts changes (as shown below). If there are too many students, invite
some of them to pick paper strips randomly; if there are only a few students, then all the students
have a chance to pick paper strips. Following the instructions on the paper strips, students should
count the current number of the nuts, erase the previous number and write down a new number with
pencils.
If the maximum temperature today exceeds 5℃, the squirrel will pick 10 nuts outdoors.
In the afternoon, a little bird pays a visit and gives the squirrel 5 nuts.
Today is the monkey’s birthday. The little squirrel gives him 3 nuts as a gift.
3
-
The squirrel plays the rock-paper-scissors game with a friend three times. If he loses the
game, he gives away a nut; if he wins the game, he gets a nut; if it’s a draw, then no gain or loss
It’s hot today. The squirrel buys a straw hat from Aunt Bear with 7 nuts.
If the amount of the nuts in the box is less than 5, then the squirrel will pick 10 nuts outdoors.
If the amount of the nuts in the box exceeds 12, then the little squirrel will turn in a circle
happily.
Ask students
1. What’s the variable in the game? What name will you give it?
4. How did you compare the variable with other values in the game?
Tips:
The order of how the paper strips are picked affects the calculated results. In some cases,
students might get a negative number after they calculate the nuts. If students have already learned
about the concept of a negative number, tell them to replace the previous value with a negative
number; if they haven’t learned the concept yet, then teachers should revise the conditions on the
Summary Imagine that a variable is a box and you can store a value that can be changed in
the box. When you need to use the variable in a program, the box will open and the value will be
taken out. Next, you can add, subtract, multiply or divide the value, or compare the value with
another value. By doing this, you get a new value and can replace the original value with a new one.
Step 4 Tasks
Guided by teachers, students learn how to create, set and use a variable. Tell them to tick the
square box when they finish the step (The Tasks Cards are included at the end of this
4
-
Instruction:
1. How to create a variable: Open the mBlock 5, go to the category Variables and select Create
Variable.
2. Set a value:
Have students work in pairs or work on their own to complete the tasks.
Codey Rocky turns left 70 degrees to face towards the left and then turns right 140 degrees
to face towards the right. Finally, it turns left 70 degrees to return to the starting place. To make
Codey Rocky turn specific degrees, we have to create a variable named “angle”.
Assign a new value to the variable “angle” to make Codey Rocky turn right 140 degrees
Tips
5
-
1. After we create a variable, new blocks will show up in the Scripts area:
Variable block:
decrease
3. In the program, the variable has three different settings and is used three times.
Sample Program
Discussion
1. I want to make Codey Rocky move forward at a certain speed and make it last for 1 second.
6
-
Codey Rocky have learned the basics of operations and knew how to add 1 to a value and
Codey Rocky would like to start with the number 0, so we can set the value of the variable
“number” to be 0.
When the button B is pressed, the number decreases by 1. A negative number refers to
subtracting.
If you would like to show each calculated result on the LED display, you can add the block
Tips:
2. The parameter dent has three shapes: hexagon, rectangle, and round rectangle. A variable
Sample Programs
7
-
When the button A is pressed, the number is multiplied by 2. We need to use the operator
block of multiply here to get a calculated result. Then, assign the result to the variable.
Show each calculated result on the LED display. It is worth pointing out that the LED display
Challenge Change an initial number and a factor (replace the number 2 with another
Tips
1. We need to use the Operators blocks to get calculated results. We replace the previous value
2. Make sure the variable you use under the three different events (when Codey Rocky starts
up; when the button A is pressed; when the button B is pressed) is the same one.
Sample Programs
8
-
Discussion
1. If we have a closer look at the values on the LED display, we will notice one thing: if there
is a time that the calculated result includes a decimal, then the following calculated results will
Task 4 Speed up
Task 4-Speed up
Classes are over. Codey Rocky is speeding up and rushing home at its top speed.
The initial speed of Codey Rocky is set to be 0, which means that Codey Rocky stays there
not moving.
When the button A is pressed, the speed will keep rising by 20 at a time. Repeating 5 times,
If the speed exceeds 70, the RGB LED will light on with color red to give a warning. In this
case, we need to use the comparison operator block to tell whether the conditional statement
is true or not.
Challenge Set the speed to be 100 and then keep slowing the speed until making Codey
Rocky stop.
Tips
9
-
1. The top power of Codey Rocky is 100% . If a power value exceeds 100%, Codey Rocky
2. The minimum power of Codey Rocky is 0%. If a power value is a negative number, then
Codey Rocky will move in an opposite direction. In other words, it will move backward.
3. Make the LED display show the value promptly the moment the variable changes.
Sample Programs
Discussion
1. Set the initial speed of Codey Rocky to be 100 and keep lowing the speed until it stops. How
2. Set the power to be a negative number. Observe how Codey Rocky will move?
Rules Codey is counting down. During the period, anyone who is holding Codey has to
give an animal name. The game ends the moment the bomb blasts.
When Codey Rocky starts up, the initial value of the variable “timer” is set to be 30.
When the button A is pressed, the “timer” will start counting down. After 1 second, the value
Add sounds to make the countdown more intense and display how much time is left.
During the countdown period, anyone who is holding Codey has to give an animal name
and then passes Codey to the next person. Animal names cannot be repeated.
When the value of “timer” becomes 0, the countdown ends and the bomb blasts. At this
Challenge Reset the initial value of the variable “timer” to make the game time longer or
shorter.
Sample Programs
11
-
Rules When the button is pressed, Codey will generate a number randomly. Have two
students play the game rock-paper-scissors. Anyone who loses the game has to press the
button. And the number on the LED display will decrease by 1. Repeat the steps until the
display shows the number that Codey randomly generates. Boom! The bomb blasts.
We need to create two variables: bomb and number. The variable “bomb “represents the
randomly generated number. The variable “number” indicates the changing numbers (starting
from 0). The bomb blasts when the two variables have the same values.
When Codey Rocky starts up, the initial values of the two variables are 0.
When the button A is pressed, the value of “bomb” is set to be a random number that falls
Meanwhile, Codey puts on his sunglasses and plays the sound “ready”.
Two students play the game rock-paper-scissors. Anyone who loses the game has to press the
button B. When the button is pressed, the value on the LED display increases by 1 and the new
If the value of the variable “number “ is the same one as the “bomb“ value, an image
bomb will appear on the LED display and then comes the sound. You will have to use the operator
blocks of comparison and conditional blocks to assess whether the two variables are identical in
values.
12
-
Sample Programs
Task 7 Rock-Paper-Scissors
Task 7-Rock-Paper-Scissors
When Codey Rocky starts up, the initial values of all the variables are set to be 0.
Codey Rocky forms one of the shapes randomly. It uses the number 0, 1 and 2 to represent
rock, paper and scissors respectively. When Codey is shaken, the value of the variable “shape”
If the value of the variable “shape” is set to be 0, then an image of a fist will appear on the LED
display.
If the value of the variable “shape” is set to be 1, then an image of scissors will appear on the
LED display.
13
-
If the value of the variable “shape” is set to be 2, then an image of paper will appear on the LED
display.
If Codey wins the game, then press the button A. The variable “win” will increase its value
If Codey loses the game, then press the button B. The variable “lose” will increase its value
If it is a draw, then press the button C. The variable “draw” will increase its value by 1.
In some cases, Codey might peep at its winning probability. When the light intensity exceeds
2, the winning probability will appear on the LED display in the form of decimals.
Tips
1. When we write programs for Codey Rocky, we can use six events at most.
Sample Programs
14
-
Discussion
1. How to show the winning probability on the LED display in the form of percentages.
Sample answer:
2. How to show the winning probability in the form of ratio? Sample answer:
15
-
Rules: When the button is pressed, Codey transmits an infrared signal; if another Codey
receives the signal, then the health points go down by 1; the game ends when the health
points fall to 0.
The two ears of the Codey are preset with an IR receiver and an IR transmitter, which
Select the block “send IR message” in the category “Infrared”. Have two Codeys send
each other different messages. In this way, they detect each other. For instance, two Codeys send
messages “red” and “blue “respectively. The messages serve as the bullets.
When one Codey receives the message from another Codey, then we consider the first Codey
gets shot and its health points reduce by 1. We use the conditional block here to assess whether
the Codey receives an IR message (gets shot). If Codey gets shot, it will make a scary sound and
When Codey starts up, the initial value of the health point should be set to 10.
The next step is to use the forever block to keep detecting whether Codey gets “bullets” from its
“enemy”. If Codey gets shot, the health point increases by “-1” and plays the sound “scary”. The
game is over when the health points fall to 0 and Codey looks so sad.
16
-
When the button A is pressed, if Codey still has health points (the value is greater than 0),
Tips
1. When we write programs for Codey Rocky, we can use six events at most.
Sample Programs
Discussion
1. If there are three teams, then how should we revise the programs?
17
-
Self-review Report
Name Age
Describe what you like most and least about this class session briefly.
Why do we say a variable is a What does the variable store How do you use variables in
You can draw out how you feel about this class session at the upper right corner of the self-review
report.
18
-
If the maximum temperature today exceeds 5℃, the squirrel will pick 10 nuts outdoors.
In the afternoon, a little bird pays a visit and gives the squirrel 5 nuts.
Today is the monkey’s birthday. The little squirrel gives him 3 nuts as a gift.
The squirrel plays the rock-paper-scissors game with a friend three times. If he loses the
game, he gives away a nut; if he wins the game, he gets a nut; if it’s a draw, then no gain or loss
It’s hot today. The squirrel buys a straw hat from Aunt Bear with 7 nuts.
If the amount of the nuts in the box is less than 5, then the squirrel will pick 10 nuts
outdoors.
If the amount of the nuts in the box exceeds 12, then the little squirrel will turn in a circle
happily.
19
-
Codey Rocky turns left 70 degrees to face towards the left and then turns right 140 degrees
to face towards the right. Finally, it turns left 70 degrees to return to the starting place. To make
Codey Rocky turn specific degrees, we have to create a variable named “angle”.
Assign a new value to the variable “angle” to make Codey Rocky turn right 140 degrees
20
-
Codey Rocky have learned the basics of operations and knew how to add 1 to a value and
Codey Rocky would like to start with the number 0, so we can set the value of the variable
“number” to be 0.
When the button B is pressed, the number decreases by 1. A negative number refers to
subtracting.
If you would like to show each calculated result on the LED display, you can add the block
21
-
When the button A is pressed, the number is multiplied by 2. We need to use the operator
block of multiply here to get a calculated result. Then, assign the result to the variable.
Show each calculated result on the LED display. It is worth pointing out that the LED display
Challenge Change an initial number and a factor (replace the number 2 with another
22
-
Task 4-Speed up
Classes are over. Codey Rocky is speeding up and rushing home at its top speed.
The initial speed of Codey Rocky is set to be 0, which means that Codey Rocky stays there
not moving.
When the button A is pressed, the speed will keep rising by 20 at a time. Repeating 5 times,
If the speed exceeds 70, the RGB LED will light on with color red to give a warning. In this
case, we need to use the comparison operator block to tell whether the conditional statement
is true or not.
Challenge Set the speed to be 100 and then keep slowing the speed until making Codey
Rocky stop.
23
-
Rules Codey is counting down. During the period, anyone who is holding Codey has to
give an animal name. The game ends the moment the bomb blasts.
When Codey Rocky starts up, the initial value of the variable “timer” is set to be 30.
When the button A is pressed, the “timer” will start counting down. After 1 second, the value
Add sounds to make the countdown more intense and display how much time is left.
During the countdown period, anyone who is holding Codey has to give an animal name
and then passes Codey to the next person. Animal names cannot be repeated.
When the value of “timer” becomes 0, the countdown ends and the bomb blasts. At this
Challenge Reset the initial value of the variable “timer” to make the game time longer or
shorter.
24
-
Rules When the button is pressed, Codey will generate a number randomly. Have two
students play the game rock-paper-scissors. Anyone who loses the game has to press the
button. And the number on the LED display will decrease by 1. Repeat the steps until the
display shows the number that Codey randomly generates. Boom! The bomb blasts.
We need to create two variables: bomb and number. The variable “bomb “represents the
randomly generated number. The variable “number” indicates the changing numbers (starting
from 0). The bomb blasts when the two variables have the same values.
When Codey Rocky starts up, the initial values of the two variables are 0.
When the button A is pressed, the value of “bomb” is set to be a random number that falls
Meanwhile, Codey puts on his sunglasses and plays the sound “ready”.
Two students play the game rock-paper-scissors. Anyone who loses the game has to press the
button B. When the button is pressed, the value on the LED display increases by 1 and the new
If the value of the variable “number “ is the same one as the “bomb“ value, an image
bomb will appear on the LED display and then comes the sound. You will have to use the operator
blocks of comparison and conditional blocks to assess whether the two variables are identical in
values.
25
-
Task 7-Rock-Paper-Scissors
When Codey Rocky starts up, the initial values of all the variables are set to be 0.
Codey Rocky forms one of the shapes randomly. It uses the number 0, 1 and 2 to represent
rock, paper and scissors respectively. When Codey is shaken, the value of the variable “shape”
If the value of the variable “shape” is set to be 0, then an image of a fist will appear on the LED
display.
If the value of the variable “shape” is set to be 1, then an image of scissors will appear on the
LED display.
If the value of the variable “shape” is set to be 2, then an image of paper will appear on the LED
display.
If Codey wins the game, then press the button A. The variable “win” will increase its value
If Codey loses the game, then press the button B. The variable “lose” will increase its value
If it is a draw, then press the button C. The variable “draw” will increase its value by 1.
In some cases, Codey might peep at its winning probability. When the light intensity exceeds
2, the winning probability will appear on the LED display in the form of decimals.
26
-
Select the block “send IR message” in the category “Infrared”. Have two Codeys send
each other different messages. In this way, they detect each other. For instance, two Codeys send
messages “red” and “blue “respectively. The messages serve as the bullets.
When one Codey receives the message from another Codey, then we consider the first Codey
gets shot and its health points reduce by 1. We use the conditional block here to assess whether
the Codey receives an IR message (gets shot). If Codey gets shot, it will make a scary sound and
the health points will fall by 1.
When Codey starts up, the initial value of the health point should be set to 10.
The next step is to use the forever block to keep detecting whether Codey gets “bullets” from its
“enemy”. If Codey gets shot, the health point increases by “-1” and plays the sound “scary”. The
game is over when the health points fall to 0 and Codey looks so sad.
When the button A is pressed, if Codey still has health points (the value is greater than 0),
then it can send IR messages and play the sound “laser”.
27