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.
Worked examples
When
tickets
price
total
new project
0
0
0
after the flag script
0
4
0
the user types 6
6
4
0
after set total
6
4
24
Trace the two scripts. Click the flag first. Then click the sprite and type 6. The three variables change like this.
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?
cakes is 7, cost is 3 and bill is 21, because 7 × 3 = 21. The sprite says Pay: 21.
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?
It would say Total: 0. The price was never set to 4, so it is still 0, and 5 × 0 = 0.
3
Draw the flowchart for Script 1, the green flag script.
Start, set tickets to 0, set price to 4, set total to 0, stop. All three steps are rectangles because they are processes.
4
Which of the three variables does the click script change, and which one does it only read?
It changes tickets and total. It only reads price, because price is used in the calculation but never changed.