Skip to the main content
PeabodyMake a free story

🤖 Code & Robots, level 5

What the trails say and what they ask. Print it for the class, or put the same questions on the board with a live quiz.

Code & Robots, level 5

📦 Variables: A Box That Remembers

How does a video game remember your score?

  • How does a video game remember your score? It uses a variable. A variable is like a box with a name written on the outside, and inside the box is a value, such as a number. A game might have a variable named score. When the game begins, the program puts a zero inside it, and coders write that as score = 0. After that, the game can look inside the box whenever it needs to know your score.
  • The value inside a variable can change, which is just why it is called a variable. Every time you catch a star, the program runs score = score + 1. That line means: look inside the box, add one to the number you find there, and then put the new number back. If the score was 0, it becomes 1. When you catch another star, it becomes 2, and the number keeps growing as you play.
  • A variable only holds one value at a time. When a new value goes in, the old value is gone for good. If a program says lives = 3 and then lives = lives - 1, the box now holds 2, not 3. Coders follow each line in order, one by one, and write down the value after every single step. This careful checking is called tracing, and it helps them find mistakes in their programs before anyone plays the game.

Try it at home: Keep score in a card game on paper: write score = 0, then score = score + 1 for each point, and trace it together.

Questions and answers

Variables: A Box That Remembers

  1. 📦 A variable is like a… box with a name
  2. score = 2, then score = score + 1. What is score now? 3 (Score was 2, and adding 1 makes it 3.)
  3. What does a variable hold? A value, like a number
  4. 📦 x = 5, then x = x + 2. What is x now? 7 (x was 5, and adding 2 makes it 7.)
  5. points = 4, then points = 9. What is points now? 9 (A box holds one value. When 9 goes in, the 4 is gone.)
  6. 🔄 Why is it called a variable? Its value can change