Student workbook - Unit 3 Variables
Student workbook - Unit 3 Variables
Science
Unit 3:
Variables
Student workbook
makecode.microbit.org
Table of Contents
Overview......................................................................................................................................... 3
Unit summary............................................................................................................................... 3
Learning goals.............................................................................................................................. 3
Lesson A: Variables in daily life....................................................................................................... 4
Lesson B: Make a game scorekeeper.............................................................................................. 6
Lesson C: Everything counts......................................................................................................... 15
Glossary of key terms................................................................................................................... 22
Overview
Learning goals
During this unit, you will:
Understand what variables are and why and when to use them in a program.
Learn how to create a variable, set the variable to an initial value, and change the value of
the variable within a micro:bit program.
Learn how to create meaningful and understandable variable names.
Understand that a variable holds one value at a time.
Understand that when you update or change the value held by a variable, the new value
replaces the previous value.
Learn how to use the basic mathematical blocks for adding, subtracting, multiplying, and
dividing variable values.
Apply the above knowledge and skills to create a unique program that uses variables as an
integral part of the project.
Examples
Constants: The day of the week, the year, student’s name, the school’s address, the
student’s birthday
Variables: The temperature/weather, the current time, the current class, whether members
of the class are standing or sitting…
A number variable could hold numerical data such as the year, the temperature, or the
degree of acceleration.
A string variable holds a string of alphanumeric characters such as a person’s name, a
password, or the day of the week.
A Boolean variable has only two values: true or false. For example, you might have certain
things that happen only when the variable called GameOver is false.
A sprite is a special variable that represents a single dot on the screen and holds two
separate values for the row and column the dot is currently in.
An array is another special type of variable that holds a list of multiple items.
Overview
You will be creating a program that will act as a scorekeeper for your next “Rock, Paper,
Scissors” game. You will need to create variables for the parts of scorekeeping that change over
the course of a gaming session.
1. In MakeCode, start a new project and name it something like: Scorekeeper. From the
Variables menu, make and name these three variables: PlayerAWins, PlayerBWins, PlayersTie.
In your program, you want to keep track of the number of times each player wins and the
number of times they tie. You can use the buttons A and B on the micro:bit to do this.
We already initialized these variables and now need to code to update the values at each round
of the game.
Each time the scorekeeper presses button A to record a win for Player A, you want to add
one to the current value of the variable PlayerAWins.
Each time the scorekeeper presses button B, to record a win for Player B, you want to add
one to the current value of the variable PlayerBWins.
Each time the scorekeeper presses both button A and button B at the same time to record
a tie, you want to add one to the current value of the variable PlayersTie.
Leave one block with ‘A’. Use the dropdown menu in the block to choose ‘B’ for the second
block and ‘A+B’ for the third block.
4. From the Variables menu, drag three of the ‘change variable by 1’ blocks to the coding
Workspace.
5. Place one change variable block into each of the ‘on button pressed’ blocks.
6. Choose the appropriate variable from the pull-down menus in the ‘change by 1’ blocks.
Whenever the scorekeeper presses button A, button B, or both buttons together, you will give
the user visual feedback acknowledging that the user pressed a button. You can do this by
coding your program to display:
An ‘A’ each time the user presses button A to record a win for Player A
A ‘B’ for each time the user presses button ‘B’ to record a win for Player B
A ‘T’ for each time the user presses both button A and button B together to record a tie
7. We can display an ‘A’, ‘B’, or ‘T’ using either the ‘show leds’ block or the ‘show string’ block.
To finish your program, you can add code that tells the micro:bit to display the final values of our
variables. Since we have already used buttons A and B, we can use the ‘on shake’ event handler
block to trigger this event. We can use the ‘show string’, ‘show leds’, ‘pause’, and ‘show number’
blocks to display these final values in a clear way.
8. From the Input Toolbox drawer, drag an ‘on shake’ block to the coding Workspace. Then, from
the Basic Toolbox drawer, drag a ‘show string’ block inside it.
9. From the Basic Toolbox, drag a ‘show leds’, a ‘show number’, and a ‘pause’ block to the
coding Workspace and connect them under the ‘show string’ block. Select the boxes in the
‘show leds’ block to make the letter A.
10.When you shake the micro:bit, you want it to display ‘Wins: ‘, then, display the letter A
followed by the number of wins for Player A.
11.Now, you can follow the same steps to show the number of wins for Player B.
a. From the Basic Toolbox, drag the ‘show leds’, ‘show number’, and ‘pause’ blocks to the
coding Workspace and add them under the ‘pause (ms) 1000’ block.
b. Select the boxes in the ‘show leds’ block to display the letter B.
c. From the Variables Toolbox, drag the ‘PlayerBWins’ variable to replace the 0 in the
‘show number’ block.
d. In the ‘pause’ block, use the dropdown menu, and select 1 second.
12.To show the number of ties, go to the Basic Toolbox, and drag the ‘show string’, ‘show
number’, and ‘pause’ blocks underneath the last ‘pause (ms) 1000’ block—and still inside the
‘on shake’ block.
14.There is one last block to add. Can you guess what it is? From the Basic Toolbox, drag a ‘clear
screen’ block underneath the final ‘pause’ block.
There is more you can do with the input you received using this program. You can use
mathematical operations on your variables.
Example: Perhaps you’d like to track and show the player the total number of rounds that were
played. To do this, you can add the values stored in the variables you created to keep track of
how many times each player won and how many times they tied.
15.In order to do this, you can add the code to your program under the ‘on shake’ event handler.
a. First, display a string to show the player that the following sum represents the total
number of rounds played.
b. Our program will add the values stored in the variables PlayerAWins, PlayerBWins, and
PlayersTie, and then display the sum of this mathematical operation.
c. The blocks for the mathematical operations adding, subtracting, multiplying, and
dividing are listed in the Math section of the Toolbox.
Even though there are four blocks shown for these four operations, you can access any of the
four operations from any of the four blocks, and you can also access the exponent operation
from these blocks.
Save, download, and try the program again to make sure that it runs correctly and displays the
correct numbers for each variable.
Input
Below is a reminder of all the different inputs available to you through the micro:bit:
3. Shake counter
Using the ‘on shake’ block, you can detect when the micro:bit has been shaken and increment a
variable accordingly. Try attaching the micro:bit to a lacrosse stick and keep track of how many
times you have successfully thrown the ball up in the air and caught it.
Extra mod: Have the micro:bit make a sound of increasing pitch every time you successfully
catch the ball.
4. Pedometer
See if you can count your steps while running or doing other physical activities carrying the
micro:bit. Where is it best mounted?
Extra Mod: Design a wearable band or holder that can carry the micro:bit securely so it doesn’t
slip out during exercise.
5. Calculator
Create an adding machine. Use Button A to increment the first number, and Button B to
increment the second number. Then, use Shake or Buttons A + B to add the two numbers and
display their sum.
Extra mod: Find a way to select and perform other math operations.
In any design project, it’s important to start by understanding the problem. You can begin this
activity by interviewing people around you who might have encountered the problem you are
trying to solve. For example, if you are designing a wallet, ask your friends how they store their
money, credit cards, identification, etc. What are some challenges with their current system?
What do they like about it? What else do they use their wallets for?
If you are designing something else, think about how you might find out more information about
your problem through interviewing or observing people using current solutions.
Prototype
Then, start brainstorming. Sketch out a variety of different ideas. Remember that it’s okay if the
ideas seem far-out or impractical. Some of the best products come out of seemingly crazy ideas
that can ultimately be worked into the design of something useful. What kind of holder can you
design to hold the micro:bit securely? How will it be used in the real world as part of a physical
design?
Use the simulator to do your programming and test out a number of different ideas. What is the
easiest way to keep track of data? If you are designing for the accelerometer, try to see what
different values are generated through different actions (you can display the value the
accelerometer is currently reading using the ‘show number’ block; clear the screen afterward so
you can see the reading).
Follow the design thinking approach and make sure your project meets these specifications:
Uses at least three different variables in a way that’s integral to the program
Uses unique variable names that clearly describe what information values the variables
hold
Uses mathematical operations to add, subtract, multiply, and/or divide at least two
variables
The program compiles and runs as intended and includes meaningful comments in code
Provide the written Reflection Diary entry (which we’ll talk about after you complete your
project)
Assessment
1 2 3 4
elements
Reflection Diary
Expectations
What was the problem you were trying to solve with this project?
What were the variables that you used to keep track of information?
What mathematical operations did you perform on your variables? What information did
you provide?
Describe what the physical component of your micro:bit project was (e.g., an armband, a
wallet, a holder, etc.)
How well did your prototype work? What were you happy with? What would you change?
What was something that was surprising to you about the process of creating this project?
Describe a difficult point in the process of designing this project and explain how you
resolved it.
Publish your MakeCode program and include the link.
Assessment
1 2 3 4
elements
Diary entry Diary entry is Diary entry is Diary entry is Diary entry
missing three missing two of missing one of addresses all
or more of the the required the required elements,
required elements. elements. including:
elements.
▪ Brainstormin
g ideas
▪ Construction
▪ Programmin
g
▪ Beta testing
Variables: A container for data. Every variable has a name that is used to reference the data
that it contains. Every variable also has a variable type.