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
1

Several variables

Computing 6: Unit 4, pp. 52–67
GoalFollow a program that uses three variables in two scripts and trace the values of the variables.
New words
script · a stack of blocks that starts with an event blockevent block · a block that waits for something to happen, such as the green flag being clickedmonitor · a box on the stage that shows the value of a variablereset · to put variables back to their starting values
Explanation

A program can have more than one script, and each script starts when its own event happens. In this lesson the first script starts when the green flag is clicked. It resets the variables, so every run starts in the same way. The second script starts when the sprite is clicked. It asks the user a question, stores the answer in a variable and works out a result from two variables together. A program can have as many variables as it needs. Each one has its own name and its own value. A monitor on the stage shows the value of a variable, so you can watch it change while the program runs.

Script 1whenclickedsetticketsto0setpriceto4settotalto0Script 2when this sprite clickedaskHow many tickets?and waitsetticketstoanswersettotaltotickets*pricesayjoinTotal: totalfor2seconds
Worked examples
Whenticketspricetotal
new project000
after the flag script040
the user types 6640
after set total6424
Trace the two scripts. Click the flag first. Then click the sprite and type 6. The three variables change like this.
Startask: how many tickets?set tickets to the answerset total to tickets × pricesay: Total and the totalEnd
The sprite says Total: 24 for 2 seconds. This flowchart shows the same steps as the second script. The flowchart does not show how the variables are created.
The variable price is set once, in the flag script. The click script only reads it. Keeping the price in one variable means that if the price changes, we only have to change one block.
Class activity

Ticket office (no computer): write the names tickets, price and total on three sticky notes and stick them on a table. An adult is the flag script and says ‘reset’: you write 0, 4 and 0 on the notes. A friend is the customer and says how many tickets they want. You write the number on the tickets note, work out the total by multiplying tickets by price, write it on the total note and say ‘Total: ’ and the number. Do three customers. If you have a computer or tablet, build the program in Scratch and switch the monitors on.

Practice
1
This bakery program works like the tickets program. Click the flag, and then click the sprite and type 7. What are the values of cakes, cost and bill at the end, and what does the sprite say?
Script 1whenclickedsetcakesto0setcostto3setbillto0Script 2when this sprite clickedaskHow many cakes?and waitsetcakestoanswersetbilltocakes*costsayjoinPay: billfor2seconds
2
In a new project the variables start at 0. What would the sprite say if nobody ever clicked the flag and the user typed 5?
Script 2when this sprite clickedaskHow many tickets?and waitsetticketstoanswersettotaltotickets*pricesayjoinTotal: totalfor2seconds
3
Draw the flowchart for Script 1, the green flag script.
4
Which of the three variables does the click script change, and which one does it only read?