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:
set [score] to [0]→ erases whatever is in the box and puts your value in instead. Whatever it was before, it is now0.change [score] by [1]→ adds on top of the value already in the box. Ifscoreis 5, this block makes it 6.
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
- In the Variables category, make a variable named
score. - Tick the checkbox so
scoreappears on the stage. - Choose a sprite (for example an apple or a star).
- 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:
add [Ada] to [names]→ puts a new item at the end of the list.delete [1] of [names]→ removes the item at the position you choose.
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:
- Make two variables named
scoreandclicks, and show both on the stage. - When the green flag is clicked, set both to 0.
- Every time the sprite is clicked, change
clicksby 1. - Add a condition: if the number of clicks divides evenly by 3, change
scoreby 5, otherwise change it by 1. - 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
- A variable is a named box that lets a program remember a value.
- The
set ... toblock assigns a fixed value; thechange ... byblock raises or lowers the value already there. - Ticking a variable’s checkbox shows its value to the player on the stage.
- A score-counting game is the clearest way to combine variables with loops and conditions.
- A list stores many items in order; we can add items, delete them and measure its length.
Check questions
- What is a variable, and why do we use one?
- What is the difference between
set score to 0andchange score by 0? - How do we let the player see a variable’s value on the stage?
- If
livesstarts at 3 and the blockchange lives by -1runs twice, what islives? - What is the main difference between a list and a variable?
Answers
- 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.
set ... to 0erases what is inside and puts 0 in its place.change ... by 0adds 0 to the value, so it does not change the value at all.- In the Variables category we tick the checkbox next to that variable, so its name and value appear in the corner of the stage.
livesdrops by one twice: 3 → 2 → 1. The result is 1.- 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.