Educator guide - Unit 3 Variables
Educator guide - Unit 3 Variables
Science
Unit 3:
Variables
Educator guide
makecode.microbit.org
Table of Contents
Overview....................................................................................................................................................... 3
Unit summary............................................................................................................................................. 3
Lessons....................................................................................................................................................... 3
Learning goals............................................................................................................................................ 3
CSTA K-12 computer science standards..................................................................................................... 4
Required educator preparation...................................................................................................................... 5
Preparing to lead the unit........................................................................................................................... 5
Required skills............................................................................................................................................ 5
Recommended resources........................................................................................................................... 5
Lesson A: Understanding variables................................................................................................................ 6
Lesson plan................................................................................................................................................ 6
Outline........................................................................................................................................................ 6
Before the lesson........................................................................................................................................ 6
Lesson details............................................................................................................................................. 7
Section 1: Introduction to variables......................................................................................................... 7
Section 2: Understanding the variable system........................................................................................8
Section 3: Wrap-up................................................................................................................................ 10
After the lesson........................................................................................................................................ 10
Lesson A assessments.............................................................................................................................. 10
Lesson B: Make a game scorekeeper........................................................................................................... 12
Lesson plan.............................................................................................................................................. 12
Outline...................................................................................................................................................... 12
Before the lesson...................................................................................................................................... 12
Lesson details........................................................................................................................................... 13
Section 1: Introduction.......................................................................................................................... 13
Section 2: Code with variables.............................................................................................................. 13
Section 3: Wrap-up................................................................................................................................ 24
After the lesson........................................................................................................................................ 24
Lesson B assessments.............................................................................................................................. 25
Lesson C: Everything counts........................................................................................................................ 27
Lesson plan.............................................................................................................................................. 27
Outline...................................................................................................................................................... 27
Before the lesson...................................................................................................................................... 27
Lesson details........................................................................................................................................ 27
Section 1: Introduce the lesson............................................................................................................. 27
Section 2: Make a micro:bit project using variables..............................................................................27
Section 3: Wrap-up................................................................................................................................ 32
After the lesson........................................................................................................................................ 33
Lesson C assessments.............................................................................................................................. 33
Overview
Unit summary
Introduction to Computer Science Unit 8: Booleans | 2
This unit introduces the use of variables to store
information. Students will practice giving variables
unique and meaningful names, and use basic
mathematical operations for adding, subtracting,
multiplying, and dividing variable values. In the
unplugged activity, students learn to differentiate
between constants and variables by tracking score as Once-a-week timeframe
they play a simple game of Rock Paper Scissors. Then
they code a program for their micro:bit that keeps and If your class meets only once a week,
displays the score of the game by using the this unit may take four weeks to
programmable buttons for input and the LED screen for complete.
output. In the final project, students code their own
unique program using variables, and design and build an Week 1: Lesson A: Variables in daily
object that uses the micro:bit to track score, count steps, life
turns, or something else.
Week 2: Lesson B: Make a game
scorekeeper (only up to Adding On
Lessons with Mathematical Operations)
Each of the following lessons is intended to be broken Weeks 3–4: Lesson C: Everything
into as many class sessions as needed to complete the counts (approx. 60-120 min)
activities.
Learning goals
During this unit, students 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.
1B-AP-09 Create programs that use variables to store and modify data.
2-AP-11 Create clearly named variables that represent different data types and perform operations on
their values.
Watch the correlating unit quick-start video for a brief, high level introduction to the activities and
project.
Review all materials thoroughly and become confident with content.
Practice all activities and check your programming against the solution link provided with the
coding activities.
Required skills
To lead the activities in this unit successfully, you need to be able to:
Explain the difference between constants and variables, and the following types of variables:
Number
String
Boolean
Sprite
Array
Use the following new blocks in Microsoft MakeCode:
‘make a variable’ button, ‘set variable to (0)’, and ‘change variable by (1)’ blocks from the
Variables Toolbox.
‘show string’, ‘show number’, and ‘pause (ms)’ blocks from the Basic Toolbox.
Addition blocks from the Math Toolbox.
Recommended resources
If you would like additional support to master the concepts and skills covered in this unit, review the
following resources.
Outline
Section 1: Introduction to variables
Review: Review learning goals, what the students accomplished, and preview the next lesson
Exit ticket: Distribute and collect the exit ticket to assess learning
Educator notes
1. Write the “Do now” on the board for students to think about as they arrive:
What are two physical objects that can hold items inside them?
Answer: Responses will vary, e.g., backpack, wallet, notebook, bookcase, house, car, purse, etc.
2. Introduce the lesson, learning goals, and responses to the “Do now.”
3. Introduce the concept of variables.
Overview: Variables
From our previous lesson, we learned that computer programs process information. Some of the
information that is input, stored, and used in a computer program has a value that is constant, meaning it
does not change throughout the course of the program. An example of a constant in math is “pi” because
“pi” has one value that never changes (it is 3.14). Other pieces of information have values that vary or
change during the running of a program. Programmers create variables to hold the value of information
that may change. In a game program, a variable may be created to hold the player’s current score, since
that value would change (hopefully!) during the course of the game.
Ask the students to think of some pieces of information in their daily life that are constants and others that
are variables.
What pieces of information have values that don’t change during the course of a single day (i.e., are
constants)?
What pieces of information have values that do change during the course of a single day (i.e., are
variables)?
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 they are standing
or sitting…
Variables hold a specific type of information. The micro:bit’s variables can keep track of numbers, strings,
Booleans, sprites, and arrays. The first time you use a variable, its type is assigned to match whatever it is
holding. From that point forward, you can only change the value of that variable to another value of that
same type.
Educator notes
An unplugged activity is an activity that takes place away from the computer—in other words,
“unplugged” from technology. This introduces new concepts in a fun way that gets students up and
moving, often reacting and interacting with other students face to face while playing a game or
completing a challenge. Unplugged activities allow students to practice concepts away from devices
so that when they move to coding activities, they have already walked through and thought about
the concepts on their own.
Objective
The objective of this activity is to experience creating and working with variables by pairing up and playing
Rock, Paper, Scissors.
Instructions
Play again: Tell students they will now start over and
play again for another minute. When done, ask the
students to add up their scores and how many rounds
they played.
Discussion
Educator notes
1. Review learning goals with students, what they accomplished, and preview the next lesson.
2. Distribute and collect the exit ticket.
Assessment
Review the exit ticket responses.
Reflection
Consider if there were any concepts of the lesson or exit ticket students struggled with that you
might want to reinforce or re-teach at the beginning of the next lesson.
Consider what aspects of the lesson students found engaging and how you might emphasize those
in the next lesson.
Follow up on any questions that arose during the lesson that require additional research on your
part.
Lesson A assessments
“Do now”
What are two physical objects that can hold items inside them?
Answer: Responses will vary, e.g., backpack, wallet, notebook, bookcase, house, car, purse, etc.
Format: Printed half-page handout for students to complete and turn in as they leave class. (Printer-
friendly versions are found in the assessment guide.)
Questions Answers
Number
String
Boolean
Sprite
Array
Outline
Section 1: Introduction
Review: Review learning goals, what the students accomplished, and preview the next lesson
Section 1: Introduction
Educator notes
1. Write the “Do now” on the board for students to think about as they arrive:
In programming, what’s a variable?
Answer: A variable holds the value of information that may change when the program is running.
2. Introduce the lesson, learning goals, and coding activities.
3. Discuss their responses to the “Do now.”
Educator notes
1. Use our established equipment safety procedures for distributing and using the hardware, and
ensure students are properly set up to begin coding in MakeCode and micro:bit.
2. Follow the instructions to lead the birdhouse activity. Do the activity on your device connected to
the projector or presentation screen and have students follow along as you complete each step. If
you don’t have a projector or screen, move throughout the room between steps to check for
understanding and help facilitate the activity.
3. Pause after each step to check for understanding and take questions, as needed.
4. If your program doesn’t execute properly, ask your students for help to debug and use the
solution link as needed.
5. After the activity, distribute the quiz (see the assessment guide for a printable format).
6. If you have time during class, consider reviewing the quiz answers as a group activity.
Each unit contains a micro:bit activity, which we informally refer to as a “birdhouse” activity, after
the innumerable wooden birdhouses so many of us made in wood shop as a way to master basic
skills. The activity is an example that walks students step by step through building a project that
demonstrates that lesson’s topic. By the time students finish the activity, they will have written code
that they can use in a different project of their own design.
Some students will finish the activity more quickly than others. Those students can then be a helpful
resource for their classmates, or they can challenge themselves by modifying, or “modding”, the
activity to do something different. We have provided examples and suggestions at the end of many
of these activities; feel free to suggest your own (or encourage your students to come up with their
own ideas!)
Overview
Tell the students that they will be creating a program that will act as a scorekeeper for their next Rock,
Paper, Scissors game. They will need to create variables for the parts of scorekeeping that change over the
course of a gaming session.
What would be a unique and clear name for the variable that will keep track of the number of times
Player A wins?
Student suggestions may include PAW, PlayerA, AButtonPress, AButtonCount, PlayerAWins…
Discuss why (or why not) different suggestions make clear what value the variable will hold. In
general, variable names should clearly describe what type of information they hold.
In our program, we want to keep track of the number of times each player wins and the number of times
they tie. We can use the buttons A and B on the micro:bit to do this.
Each time the scorekeeper presses button A to record a win for Player A, we 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, we 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, we
want to add one to the current value of the variable PlayersTie.
3. From the Input menu, drag three of the ‘on button A pressed’ blocks to your Workspace. 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.
Whenever the scorekeeper presses button A, button B, or both buttons together, we will give the user
visual feedback acknowledging that the user pressed a button. We can do this by coding our 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.
Notice that we added a ‘clear screen’ block after showing ‘A’, ‘B’, or ‘T’. What do you think would happen if
we did not clear the screen? Try it.
To finish our program, we 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.
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.
13. In the ‘show string’ block, change the “Hello!” to “Ties: “. Then, from the Variables Toolbox, drag the
‘PlayersTie’ variable block to replace the 0 in the ‘show number’ block. And change the ‘pause’ block to
1 second.
Try it out!
Download the Scorekeeper program to the micro:bit, and have the students play one last round of Rock,
Paper, Scissors using their micro:bits to act as the Scorekeeper.
There is more we can do with the input we received using this program. We can use mathematical
operations on our variables.
Example: Perhaps you’d like to keep track of and show the player the total number of rounds that were
played. To do this, we can add the values stored in the variables we created to keep track of how many
times each player won and how many times they tied.
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.
16. Replace the default values of 0 with the names of the variables we want to add together. Notice that
because we are adding three variables together, we need a second math block. First, we add the
values for PlayerAWins and PlayerBWins, then add PlayersTie.
Save, download, and try the program again to make sure that it runs correctly and displays the correct
numbers for each variable.
Remember: The micro:bit is a device that processes input and displays it as output in some way. By
storing values in variables, you can perform mathematical operations on that data that provides useful
information.
What other math operations could provide valuable information from the values stored in these variables?
Examples:
Calculate and display a player’s wins and/or losses as a percentage of all rounds played.
Calculate and display the number of tied games as a percentage of all rounds played.
How would you code those math operations?
If time permits: Have students code one or more of their ideas or do one as a group activity.
Section 3: Wrap-up
Educator notes
1. Review the learning goals with students, what they accomplished, and preview the next lesson.
2. Use your established equipment safety procedures for collecting and storing the hardware.
Reflection
Consider if there were any concepts of the lesson or quiz students struggled with that you might
want to reinforce or re-teach at the beginning of the next lesson.
Consider what aspects of the lesson students found engaging and how you might emphasize those
in the next lesson.
Follow up on any questions that arose during the lesson that require additional research on your
part.
“Do now”
Quiz
Format: Printed full-page handout for students to complete during class. (Please see the assessment guide
for printer-friendly versions to distribute to students.)
Questions Answers
1. What’s the difference between a constant and a A constant has a value that doesn’t
variable? change. A variable has a value that
may change.
2. Name two variables that can affect how you dress to go Responses will vary, e.g., the
outside. weather/temperature, what’s clean,
holiday season, time of day, formality
of an event, etc.
3. What’s an example of each of the following types of a. Numerical data, e.g., the year,
variables? the temperature, or the degree of
acceleration
a. Number
b. A string of alphanumeric
b. String
characters, e.g., a person’s name,
c. Boolean a password, or the day of the
week
c. Only two values: true or false
(can also be represented by
0=false, 1=true)
4. Why is it important to name variables in a clear and Variable names should clearly
meaningful way? describe what type of information
they hold so they are easily
recognizable in the program and you
can find problems or bugs easier.
5. True or false: You can only use the default variable False. You can make a variable with
names provided in the Variables toolbox drawer. any name you want/need for a
program with the Make a Variable
button.
6. Write the pseudocode in words to describe what these Something similar to:
coding blocks do:
At the start of the program, the
variables for Player A wins, Player B
wins, and player ties are all set to
zero.
Outline
Section 1: Introduce the lesson
Exit ticket: Students complete a Reflection Diary entry for their project
Review: Review the learning goals, what students accomplished, and preview the next unit
Educator notes
1. Write the “Do now” on the board for students to think about as they arrive:
What’s a physical object that counts, holds, or tracks values that you might prototype?
Answer: Responses will vary, e.g., wallet, scorecard, fitness tracker, calculator, etc.
2. Introduce the lesson, learning goals, and discuss responses to the “Do Now.”
3. Review any themes that students are struggling with from the quiz results unless you covered
this at the end of the last lesson.
Educator notes
1. Use your established equipment safety procedures for distributing and using the hardware and
ensure students are properly set up to begin coding in MakeCode and micro:bit.
2. Follow the instructions to explain the expectations for the binary cash register project, the
example, and the scoring rubric, which is also in the student workbook.
3. Encourage students who finish early or need an extra challenge to try one or more of the mod
ideas and/or offer the optional additional binary wristwatch project.
4. After the activity, consider having students share their projects in pairs, small groups, or with the
rest of the class as time allows.
5. Be sure to allow enough time for students to complete their Reflection Diary entry in the next
section.
Objective of projects
Open-ended projects are opportunities to apply the concepts and skills students have developed in
an original and creative way. Students will work on their projects in a “collaboratively independent”
way, which means each student is responsible for turning in his or her own project but is encouraged
to work together and help their peers while doing so. Some form of reflection is an important part of
documenting the learning that has taken place.
Input
Remind the students of all the different inputs available to them 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
Variable names None of the A minority of The majority of All variable names
variable names variable names variable names are unique and
clearly describe are unique and are unique and clearly describe
what information clearly describe clearly describe what information
values the what information what information values the
variables hold. values the values the variables hold.
variables hold. variables hold.
micro:bit program micro:bit program micro:bit program micro:bit program micro:bit program:
lacks three or lacks two of the lacks one of the
more of the required elements. required elements. Uses variables
required elements. in a way that is
integral to the
program
Uses
mathematical
operations to
add, subtract,
multiply, and/or
divide variables
Compiles and
runs as
intended
Uses
meaningful
Section 3: Wrap-up
Educator notes
1. Explain the expectations and scoring rubric for the Reflection Diary entry for their project, which
is also in the student workbook.
2. Review the learning goals with students, what they accomplished, and preview the next unit.
3. Use your established equipment safety procedures for collecting and storing the hardware.
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 or missing two of the missing one of the addresses all
more of the required elements. required elements. elements.
required elements.
Assessment
Review and grade the projects and diary entries. (A full page, printer-friendly version of the project
and diary entry scoring rubric is available in the assessment guide.)
Reflection
Consider if there were any concepts of the lesson, project, or diary entries students struggled with
that you might want to reinforce or re-teach before or at the beginning of the next unit.
Consider what aspects of the lesson students found engaging and how you might emphasize those
in the next unit.
Follow up on any questions that arose during the lesson that require additional research on your
part.
Lesson C assessments
“Do now”
What’s a physical object that counts, holds, or tracks values that you might prototype?
Answer: Responses will vary, e.g., wallet, scorecard, fitness tracker, calculator, etc.
Write a diary entry of about 150–300 words addressing the following points:
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.