You are on page 1of 6

Lesson 3 - Variables

(Levels 15 - 17)

Summary

Working with variables is like organizing things in shoeboxes. You give the shoebox a
name, like "School Supplies", and then you put things inside. The exact contents of the
box might change over time, but whatever's inside will always be called "School
Supplies". In programming, variables are symbols used to store data that will change
over the course of the program. Variables can hold a variety of data types, including
numbers and strings. In many programming languages, we need to declare the type of
data a variable can store before using it. For example, in JavaScript, a variable named
“age” which stores the value 16, has to be declared with the integer data type, by
preceding the assignment statement with the word “int”, to not result in errors. It
would look like this:

There are many other data types that can be used with variables. These types include
numbers, Booleans, lists, and strings.

The following is a list of the most common data types and what they represent:

Type Description Example


String (or str or for characters, letters, numbers and “43llo”
text) symbols.
Integer (or int) for whole numbers 44, 6, 9028
Float (or Real) for numbers that contain decimal points, 8.443, 3.5,
or fractions. 0.92
Boolean (or True/False -
bool)
For further information, share the following article with students:
https://www.bbc.co.uk/bitesize/guides/zwmbgk7/revision/3
In these levels, variables are used to name enemies, so that the hero knows which
character to attack.

Materials

● shoebox, or other small container, with a blank label


● assortment of school supplies
● Optional: Progress Journal
● Optional: Engineering Cycle Worksheet
● Optional: Python Syntax Guide or JavaScript Syntax Guide

Learning Objectives

● Create and clearly name a variable to store data.


● Use a variable as an argument in a method.
● Understand and use key terms: variable, value, data

Standards

● CSTA: 1B-AP-09 Create programs that use variables to store and modify data.
● CSTA: 2-AP-11 Create clearly named variables that represent different data
types and perform operations on their values.
● CCSS-Math: MP.1 Make sense of problems and persevere in solving them.
● CCSS-Math: MP.2 Reason abstractly and quantitatively.

Opening Activity (10 minutes):

Explain
Tell students that working with variables is like organizing things in shoeboxes in a
closet. Show an image of an organized closet full of labeled boxes, or ask students to
share personal examples of how they organize their things, highlighting examples
where they use labels to remember what is in each container. Elicit the idea that the
labels help you group similar items together, and find them again later.

Interact

Show the class your shoebox and the objects inside - pens, glue, ruler, etc. Ask them to
suggest a name for the box, and write their best idea on the label. For example, they
might label it "School Supplies".

Change some of the contents of the box, and ask students to notice that "School
Supplies" is still a clear and accurate name for the box, even though some of the
contents have changed.

Suggest some unclear or inaccurate names for the box, such as "Markers", "Stuff", or
"Snacks". Ask students what's wrong with each name, highlighting the idea that they
are too narrow ("Markers"), too broad ("Stuff"), or inaccurate ("Snacks").

Explain

Make the connection back to variables, explaining that the label "School Supplies" is the
variable and the objects inside are the value. The value can change, but whatever is in
the box will always be called "School Supplies". If you were asked to bring your school
supplies to class, you'd know to bring everything in that box.

In a computer program, variables hold data that might change over the course of the
program. For example, you might want your hero to attack the nearest enemy, no
matter who that enemy is. The nearest enemy will probably change during the level.

You define a variable by giving it a name, then setting it equal to the first value it
should hold. Display the example:

enemy = “Kratt”

Explain that the variable enemy holds (=) the value "Kratt"

Now you can use your variable in place of the value itself: hero.attack(enemy) is the
same as hero.attack("Kratt")
This is useful when there is more than one enemy in a level. For example, if you define
the variable enemy as whatever enemy is closest to your hero, like this:

enemy = hero.findNearestEnemy() hero.attack(enemy)

then you'll always attack whatever enemy is closest to you, no matter what the enemy's
name is.

Finally, mention that for now, a variable can hold only one value, even though our
shoebox could hold lots of things. Later, they'll learn ways to store multiple similar
values under one variable; for now, the value is a single number or a single string.

Discuss

Use one or more of the following discussion questions to prompt reflection:

What are variables used for?

Sample Response:

Storing a value that might change later.

How do variables in programming compare to variables in math or science?

Sample Response:

It's similar to science because in science a variable is a part of an experiment that can

be changed. Like you might have "temperature" as a variable in your experiment, and

test different values for "temperature". It's similar to math because in math a variable

is a symbol that stands in for a number that changes value. Like in x + 5 = y, the variable

x could be any value, and the value of the variable y would change too, depending on

the value of x.

Coding Time (30-40 minutes)


Tell students they will be playing Levels 15-17 today. Allow students to move through
these levels at their own pace. Circulate and assist as they work, calling attention to the
Hints button in the top right corner of each level as needed.

We recommend stopping students after Level 17 and using the next lesson plan to introduce
the next concepts before beginning Level 18.

Look Out For:

● In these levels, the values of the variables are strings. If students run into errors,
have them check that they have used quotation marks correctly.
● Loops are also needed for these levels. Have students double check that they
have correctly indented the code within their loops.
● In these levels, the enemies typically require two hits to be defeated. If their
hero does not survive, have students check to be sure they have attacked each
enemy twice.

Closure (5 minutes)

Use one or more of the following questions to prompt reflection. You can facilitate a
short discussion, or have students submit written responses on Exit Tickets.

What was the hardest puzzle you solved today? How did you solve it?

Sample Response:

15 was a hard level. There were lots of enemies and I died. So I made a loop for attack,

but I didn’t know the name of who to attack. So I clicked on the glasses and it said I

could use findNearestEnemy, but it didn’t work without saying enemy =. Then I could

attack(enemy) and it worked.

What does the method findNearestEnemy do? When do you use it?

Sample Response:
The hero can see which enemy is closest by writing hero.findNearestEnemy(). But you

have to name a variable for it so you can call it. You can say enemy =

hero.findNearestEnemy(). Then you can attack the closest enemy on the next line by

saying hero.attack(enemy). If you put both of those lines inside a loop, the program

will keep updating who the nearest enemy is, and you'll keep attacking new enemies!

Differentiation

Additional Supports:

● Show students how to find the hints, methods reference cards, error messages,
and sample code provided within each level.
● Students struggling with a given level will be automatically directed to additional
practice levels within the game.
● If you would like students to take notes as they work, a printable template is
available here: Progress Journal
● If students struggle with breaking down problems, you can use the printable
Engineering Cycle Worksheet to reinforce a step-by-step problem-solving
approach.
● If students struggle to follow correct syntax, provide a copy of the printable
Python Syntax Guide or JavaScript Syntax Guide

Extension Activities:

● This lesson uses a shoebox analogy. Have students come up with their own
analogies by completing the following sentence frame in as many ways as
possible: A variable is like a (blank) because (blank). To push them creatively, set
a minimum of 8 ideas, to help them get past their first or most obvious idea.

You might also like