← Back to course
Code Crew G3 / Week 04 / Tuesday
2/6
Week 04 Β· Variables

Tuesday

// Boxes that hold things

Tuesday: Cup & Counter

Rocket sets three paper cups on the table. He names them score, lives, and level.

"Score holds zero," Rocket says. "Lives holds three. Level holds one."

Raven watches the game begin. "What do you notice when something happens?" she asks.

Nova hums. "Each event changes one cup," she says. Let's update each cup as the game goes on.

Imagine three paper cups. Each cup has a name. Right now each cup holds a number.

score   = 0
lives   = 3
level   = 1

Update each cup

After Rocket scores a goal:

score =

After Raven loses a life:

lives =

After both finish a level:

level =
Try Without Paper
Find 3 things in your room you could be a 'cup' for: a piggy bank,
a chore chart, the days till your birthday. They're all variables.

Many Boxes at Once

A program can have many variables. Each one has its own name, so they never mix up.

Change one box, and the other boxes stay the same.

score = 0
lives = 3
level = 1
score = score + 5
lives = lives - 1
After all five lines, what is score?
What is lives?
What is level?
BOXES CHECK
  • Read the question.
  • Tap your answer.
Which box did NOT change?
Then we add level = level + 1. What is level?
Rocket writes score = 10. What happens to the old score?
Which name is clearest for a box that counts jumps?
StatementTrue or false?
Changing score also changes lives.?
Each variable has its own name.?
lives = lives - 1 takes one away from lives.?
score = score + 5 makes score go down.?
WHY THIS EXERCISESeparate names keep separate values safe.
Try it
Use three cups for score, lives and level.
Play a pretend game round with a grown-up.
Update each cup after every move.

Quick Check

The score cup holds 2. Rocket scores a goal. Now score holds ____.
The lives cup holds 3. Raven loses a life. Now lives holds ____.
← Monday