AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Variables in Scratch

Week 9 · Unit 4: Variables in programming · 3 lessons
2

Make a variable

Computing 6: Unit 4, pp. 52–67
GoalSay how a variable is made in Scratch and which blocks and categories to use for asking, storing and showing data.
New words
category · a group of blocks of the same colour, such as Motion or Variablesanswer · a Scratch block that holds what the user typed in reply to a questionjoin · an operator block that sticks two pieces of text togetherfor this sprite only · a choice that makes a variable belong to one sprite
Explanation

The blocks in Scratch are sorted into coloured categories. Events blocks are yellow, Motion blocks are blue, Looks blocks are purple, Sensing blocks are light blue, Operators blocks are green and Variables blocks are orange. To make a variable, open the Variables category and click Make a Variable. Type a clear name. Then choose For all sprites, so that every sprite can use it, or For this sprite only. The new variable then appears with its own blocks. Use set to give it a value and change by to add to it. The block ask and wait is in Sensing, and it stores the reply in the block answer. The block join, in Operators, sticks pieces of text together.

BlockCategoryWhat it does
ask [ ] and waitSensingasks a question and waits for a reply
(answer)Sensingholds the last reply that was typed
(join [ ] [ ])Operatorssticks two pieces of text together
set [ ] to ( )Variablesputs a value in a variable
change [ ] by ( )Variablesadds a number to a variable
say [ ] for ( ) secondsLooksshows a speech bubble
Worked examples
StepWhat to do
1Click the Variables category.
2Click the button Make a Variable.
3Type the name tickets.
4Choose For all sprites or For this sprite only.
5Click OK. The new blocks and a monitor appear.
This is how to make a variable called tickets. Each step is one click or one choice.
show variableticketshide variabletickets
The blocks show and hide control the monitor on the stage. A monitor is useful while you test a program, and you can hide it when the program is finished.
setnametoanswer
A block that is a reporter, such as (answer) or (tickets), fits into a round slot of another block. This block puts the user’s reply into the variable name.
Class activity

Paper blocks (no computer): cut out strips of coloured paper and write one block on each: when this sprite clicked, ask [What is your name?] and wait, set [name v] to (answer), say (join [Hello ] (name)) for (2) seconds. Colour them by category with pencils: yellow for Events, light blue for Sensing, orange for Variables and purple for Looks. Mix them up, and put them in the right order. Then act it out: an adult is the user and answers the question, and you say the greeting. If you have a computer or tablet, open Scratch and find each block.

Practice
1
Which category is each block in? (a) ask [ ] and wait, (b) say [ ] for ( ) seconds, (c) set [ ] to ( ), (d) join, (e) when green flag clicked.
2
Which block is missing from this script so that the sprite can greet the user by name?
Greeterwhen this sprite clickedaskWhat is your name?and wait?sayjoinHello namefor2seconds
3
When is it better to make a variable ‘for this sprite only’?
4
The monitor shows score 0. These blocks run. What does the monitor show at the end?
changescoreby1changescoreby1