Home · Academy · Robotics & Coding · Programming with Scratch · Variables and Lists

Variables and Lists

Create and update variables for scores and counters, with a short intro to lists.

LESSON COMPASS

What will you use this page for?

Core idea

A variable is a box that lets a program remember a number or a piece of text, while a list is a longer set of boxes that stores many values in order.

Evidence to produce

Complete the page task with your own input, test conditions and reasoning.

Control trap

Using “change by” instead of “set” (or the reverse) If you write change score by 1 at the start of a game, the score piles onto the previous value instead of starting from zero. Always use the set ... to block for the starting value. Forgetting to reset the score If the green-flag block has no set score to 0 , the…

Next connection

Broadcasting Messages and Managing the Stage: projects with several scenes where sprites send signals to one another and switch between backdrops.

Module sources: Python Tutorial · Arduino Learn

LevelBeginner
Age10–16
Duration30–45 min
PrerequisiteLoops in Scratch
ContentStandard lesson · 1,591 words
Last updated

One-sentence summary

A variable is a box that lets a program remember a number or a piece of text, while a list is a longer set of boxes that stores many values in order.

Why it matters

In the previous lesson we used loops to run the same steps again and again. But most games need the program to remember something: how many points you scored, how many lives you have left, which level you are on.

Scratch does not keep this information on its own. We have to tell it, “store this number somewhere and change it when I ask.” That storage box is called a variable.

Once you understand variables, you can build more than a character that moves. You can build real games that count points, level up and run a countdown. That is why variables are the key that takes you from your first simple animation to your first real game.

What is a variable?

A variable is a named box that holds a value, and that value can change later. The box has a name (for example score) and it holds a value inside it (for example 0).

To create a variable in Scratch, we open the Variables category and click “Make a Variable.” We give it a clear name: score, lives, time.

The difference between “set” and “change by”

The Variables category has two key blocks, and mixing them up is very common:

Let us see an example. At the start of a game, we want to reset the score:

when green flag clicked
set score to 0

During the game, we want to raise the score by one on every correct move:

when this sprite clicked
change score by 1

The difference matters: set assigns a fixed value, while change by raises or lowers the value that is already there. To lower it, we use a negative number: change lives by -1.

Showing a variable on the stage

Next to every variable there is a small checkbox. If you tick it, the variable’s name and value appear in the corner of the stage. For example, the player sees score: 7 update live on screen.

If you untick the box, the variable still works; it just does not appear on screen. When we want the player to see the score, we leave the checkbox on.

Your first game: points on every click

Now let us combine what we have learned into a small game. The goal is simple: every time you click a target on screen, the score goes up by one.

Step-by-step setup

  1. In the Variables category, make a variable named score.
  2. Tick the checkbox so score appears on the stage.
  3. Choose a sprite (for example an apple or a star).
  4. Build the two block stacks below.

Reset the score when the game starts:

when green flag clicked
set score to 0

Raise the score and give a small reaction every time the sprite is clicked:

when this sprite clicked
change score by 1
start sound Meow
change size by 20 for 0.1 seconds
wait 0.1 seconds
set size to 100 %

When you press the green flag, score becomes 0. Every time you click the apple, score goes up by one and the apple grows and shrinks for a moment to react. That is your first score-counting game.

Making it harder: a countdown

Real games often have a time limit. Let us add a second variable: time.

when green flag clicked
set time to 20
repeat 20
  wait 1 seconds
  change time by -1
say "Time is up"

Here we combined the repetition structure from the loops lesson with a variable. The time drops by one each second; when it reaches zero the game ends. Two variables (score and time) run at the same time: one goes up, one goes down.

A short introduction to lists

Sometimes a single value is not enough. In a quiz game you may need to store many questions, or in a high-score table you may need many player names. Making a separate variable for each one would be exhausting.

A list is a set of boxes that stores many values in order. Each value in a list is called an item, and each item has a position number: item 1, item 2, item 3, and so on.

To create a list in Scratch, we again use the Variables category and click “Make a List.” For example, let us make a list named names.

Adding and deleting items

The two list blocks you will use most are:

Example: let us collect player names into a list.

when green flag clicked
delete all of names
add Ada to names
add Bora to names
add Ceren to names

After these blocks run, the list looks like this: 1. Ada, 2. Bora, 3. Ceren. The length of names block tells you how many items are in the list (here, 3).

If you want to remove an item:

delete 2 of names

Now only Ada and Ceren are left. A variable holds a single value; a list holds many values in a tidy order. Both let information stay in the program’s memory.

Mini practice

Improve your own click game with two variables:

  1. Make two variables named score and clicks, and show both on the stage.
  2. When the green flag is clicked, set both to 0.
  3. Every time the sprite is clicked, change clicks by 1.
  4. Add a condition: if the number of clicks divides evenly by 3, change score by 5, otherwise change it by 1.
  5. Test the game with the green flag a few times and confirm the score rises the way you expected.

When you finish, ask yourself: after 6 clicks, what should the score be? Did it really reach that number?

Common mistakes

Using “change by” instead of “set” (or the reverse)

If you write change score by 1 at the start of a game, the score piles onto the previous value instead of starting from zero. Always use the set ... to block for the starting value.

Forgetting to reset the score

If the green-flag block has no set score to 0, the score in a second round continues from where the last one left off. Reset your variables so every round starts clean.

Forgetting to clear the list

If the game adds items to the list every time it starts, after a few tries the same names pile up again and again. Use the delete all of ... block first.

Choosing meaningless names

A name like variable1 does not remind you what it does. If you choose clear names such as score, lives and time, your code explains itself.

Safety note

Scratch is an app you use in front of a screen. To avoid staring at the screen without a break, pause briefly every 30 minutes and rest your eyes. When you share your projects, do not put your real surname, school, address or phone number inside a variable or a list; use only a nickname instead. Ask an adult before you share a project online.

Lesson summary

Check questions

  1. What is a variable, and why do we use one?
  2. What is the difference between set score to 0 and change score by 0?
  3. How do we let the player see a variable’s value on the stage?
  4. If lives starts at 3 and the block change lives by -1 runs twice, what is lives?
  5. What is the main difference between a list and a variable?

Answers

  1. A variable is a named box that stores a value (a number or text) in memory. We use it so the program can remember information such as score, lives or time.
  2. set ... to 0 erases what is inside and puts 0 in its place. change ... by 0 adds 0 to the value, so it does not change the value at all.
  3. In the Variables category we tick the checkbox next to that variable, so its name and value appear in the corner of the stage.
  4. lives drops by one twice: 3 → 2 → 1. The result is 1.
  5. A variable holds a single value; a list holds many values as ordered items, and each item has a position number.

Source and verification note

For “Variables and Lists”, verification focuses on whether the relationship between What is a variable? and Showing a variable on the stage remains consistent across examples. Block names are kept consistent with the current core Scratch categories. Project behaviour should be tested separately for start-up, normal play, errors and restarting.

Next lesson

Broadcasting Messages and Managing the Stage: projects with several scenes where sprites send signals to one another and switch between backdrops.

Start QuizBack to Programming with Scratch
QUESTION POOL

Reinforce this lesson with 10 questions

This lesson has a pool of 20 questions. Each attempt selects 10 and reshuffles the choices.