Skip main navigation

Using variables in a Scratch program

In Scratch you can do more than just set and change variables; you can use them in calculations and comparisons, or store user input - read more here.

In this step, I’m going to show you more things that you can do with the variables you’ve created in Scratch. As well as using the change block, you can use other mathematical operators to manipulate numeric variables. You can also use selection to make decisions based on the value of a variable.

Changing variable values

You may want to make a variable change by a regular amount, for which you can use a count-controlled loop. The code below:

  • Sets my number to 0 before the loop
  • Runs the loop 10 times:
    • Waiting for a second each time so that you can see the current value of my number
    • Increasing the value of my number by two each time

When the loop finishes running, the final value of my variable is 20.

Scratch screen recording showing the following code running - when this sprite clicked, set my number to 0, repeat 10, wait 1 second, change my number by 2. When the green button on the stage is clicked the variable changes value in the sequence 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20.

Operators in Scratch

Scratch includes an Operators category with blocks that allow you to perform calculations or compare values in your programs. The four blocks for performing simple calculations are add +, subtract -, multiply * and divide /.

Scratch operator blocks +, -, * and /

You can use these blocks to set a variable’s value:

Scratch blocks - set (my number v) to (10), set (my total v) to ( (my number) * (8) )

In the example above, Scratch will set my number to 10 when it runs the first block. When it runs the second block, it will set the value of my total to my number multiplied by 8, which is 80. my number will keep the value 10.

You can even set a variable to a new value, based on the current value of that variable:

Scratch blocks - set (my number v) to (50), set (my number v) to ( (my number) / (2) )

In this example, Scratch will set my number to 50 when it runs the first block. When it runs the second block, Scratch will take the current value of my number, which is 50 from the first set … block, and divide it by 2 making the new value 25. It will then set my number to this new value, so my number will finish this sequence with the value 25.

Random numbers

The pick random operator allows you to generate random numbers in your programs. When you use the block you need to set the range of numbers that Scratch will choose from. In the example shown, Scratch will pick a number between (and including) 1 and 10.

Note: If you set the range using whole numbers, Scratch will pick a random whole number. If you set the range to include decimal places e.g. 0.5 to 10 Scratch will pick a random number between those values with six decimal places.

Scratch blocks - pick random (1) to (10), set (my number v) to (pick random (1) to (10)).

You can set the value of a variable to a random number by combining set my variable to with the pick random block.

User input

Scratch includes a number of built-in variables that you can use as a programmer. Earlier this week you created a quiz and used the ask … and wait block. When a user types in a response to ask …, Scratch stores this response in a built-in variable named answer.

Two Scratch blocks - "ask … and wait", answer

You can use Scratch’s built-in variables in combination with your own. Your program might take a user’s answer and perform a calculation using it. For example, you could ask a user to enter a distance in kilometres and use their answer to calculate the equivalent distance in miles.

Scratch code - when green flag clicked, ask "Please enter a distance in kilometres" and wait, set (distance_in_miles v) to ((answer) / (1.609))

Making comparisons

Scratch includes operators which you can use in your programs to make comparisons between two values. Your program can check if a value is equal to, greater than, or less than another value.

Three Scratch Boolean blocks - equal to, greater than, less than

Using selection with the results of comparisons

I’ve created a simple program to demonstrate how you can use an if … then … else block and the Operators blocks together. My program picks a random number between 1 and 10 and stores that in a variable I’ve called my number. The program asks the user to type in a number between 1 and 10. I’ve then used selection containing a condition — “user’s answer more than the random number”. If the condition is true (the user’s number is higher), the user is told this using the first say … block. If the condition is not true, the user’s number must be the same or less than the random number, and the second say … block is run. Lastly, my program then tells the user what my number is.

(my number) then, say "Your number is more than the number I picked" for (5) seconds, else, say "Your number was the same as or less than the number I picked" for (5) seconds, end, say "My number was" for (2) seconds, say (my number) for (2) seconds”>

Next step

In the next step, I’ll show you how you can use join to get a sprite to say things which combine variables with other text. You’ll then use repetition, selection, and variables to create a times tables test.

This article is from the free online

Introduction to Programming with Scratch

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education