- Learnt30 min · ×, +, −, and one sum inside another
- Basic25 min · build the machine yourself
- Challenge 1–327 min · two prices, too little, a third coin
- Extrabonus · two snacks in one go
Review — what Level 2 gave you 5 min
Level 3 starts here. Everything below is a block you already used in Level 2 — we are going to lean on all four of them today.
- A variable is a box with a name that holds one number.
ask [What is your name?] and waitstops and waits for typing.(answer)holds whatever was typed — until the next ask.(join [Score: ] (score))glues words and numbers together.
Quick-fire
What does the bubble say?
when flag clicked
set [apples v] to (4)
say (join [I have ] (apples))
Reveal the answer
I have 4. The join takes the words on the left and the value inside the variable on the right — not the word “apples”.
Today’s Topic 3 min
- The four operators:
(() + ())(() - ())(() * ())(() / ()) - Nesting — dropping one operator block inside another
- A price, a pile of coins, and the change between them
- Turning what somebody typed into a number you can do sums with
Learning outcome
By the end of this lesson you will be able to:
- Use × to turn a count of coins into an amount of money.
- Nest one operator block inside another, and read it back out loud.
- Work out change with a single subtract.
Learnt — a machine that counts money 30 min
1 · Meet the Snack-O-Matic
You have used one of these. Coins go in, you press a code, a snack drops into the tray, and coins come back. Today you build it.
- Nine shelf slots behind the glass, in rows A, B and C.
- A price display at the top right.
- A coin slot on the right, and a tray at the bottom.
Inside a real machine there is no magic at all. There is a number for what you paid, a number for what it costs, and some arithmetic in between.
2 · A pile of coins is a multiply
Three 20c coins is not three. It is three lots of twenty. In Operators the block for “lots of” is:
(() * ())
- Make a variable called
twenties, for all sprites. - Add
ask [How many 20c coins?] and wait. - Add
set [twenties v] to (answer). - Make a variable called
money. - Drag a
(() * ())intoset [money v] to (), then drop(twenties)in the left slot and type 20 in the right.
3 · Two piles, and the first nest
Real machines take more than one coin. Ask for the 50c coins too, and the money in the slot is both piles added together.
Here is the new idea of the whole lesson. The + block has two slots — and a slot can hold another operator block. Putting a block inside a block is called nesting.
set [money v] to ((twenties) * (20))
change [money v] by ((fifties) * (50))
set [money v] to (((twenties) * (20)) + ((fifties) * (50)))
Read the nested one from the inside out: three twenties is sixty, two fifties is a hundred, sixty and a hundred is 160.
How to use this: Click the green flag, then click the stage once. Type a number and press Enter, twice — try 3 then 2.
4 · The change is a subtract
Every snack has a price. The change is what is left when you take the price off the money:
set [price v] to (45)
set [change v] to ((money) - (price))
How to use this: Click the green flag, then click the stage once. Type 3, then 2, and watch the muffin come down.
5 · The same answer, two roads
You can do today’s whole sum in one nested expression, three operators deep. It is not better — it is just shorter, and you should be able to read both.
set [change v] to ((((twenties) * (20)) + ((fifties) * (50))) - (price))
How to use this: Click the green flag, then click the stage once and type 3, then 2. Watch the two boxes on the Stage.
Basic — build the machine 25 min
Start a new project. You are building the machine from scratch, in five steps.
Step 1 — the machine
- Click the Stage, then the Backdrops tab.
- Paint a tall box: a glass window on the left, a keypad on the right.
- Paint three shelf lines across the window.
- Paint a dark tray under the window.
Step 2 — two snacks
- Choose a sprite from the library —
Muffinis a good first one. - Shrink it to about 70 and drag it onto the top shelf.
- Add a second snack —
Donut— on the same shelf, to the right.
Step 3 — the coins
- Make three variables:
twenties,fiftiesandmoney. - Tick all three so they show on the Stage.
- On the muffin, build the two asks and the two sets.
- Add
set [money v] to (((twenties) * (20)) + ((fifties) * (50))). - Press the flag and type two numbers. Check the money box is right by hand.
Step 4 — a price and the change
- Make two more variables:
priceandchange. - Set
priceto 45 at the top of the script. - After the money sum, add
set [change v] to ((money) - (price)).
Step 5 — deliver it
- Add
glide (0.6) secs to x: (-60) y: (-128)so the muffin drops to the tray. - Then
say (join [Change ] (change)) for (3) seconds. - At the very top, put the muffin back:
go to x: (-124) y: (76).
when flag clicked
go to x: (-124) y: (76)
set [price v] to (45)
ask [How many 20c coins?] and wait
set [twenties v] to (answer)
ask [How many 50c coins?] and wait
set [fifties v] to (answer)
set [money v] to (((twenties) * (20)) + ((fifties) * (50)))
set [change v] to ((money) - (price))
glide (0.6) secs to x: (-60) y: (-128)
say (join [Change ] (change)) for (3) seconds
How to use this: Click the green flag, then click the stage once. Type 3, then 2. Then press A for the muffin or B for the donut.
The money box says 0 however many coins I type.
The multiply is reading the wrong thing. Check the left slot holds the orange (twenties) variable and not the word “twenties” typed in. A typed word counts as 0 in a sum.
The second question wipes out the first number.
(answer) is one box, reused by every ask. You must set [twenties v] to (answer) before the second ask runs.
The muffin is in the tray before I have typed anything.
It ended there last run and nothing put it back. Add the go to x: () y: () under the green flag.
The change is right but the bubble says “Change (change)”.
The variable was typed into the join as text instead of dragged in. Drag the orange oval from the Variables palette.
Two snacks, two prices
One machine, two buttons, and the right price charged for each.
- An apple at 30c on one shelf, a donut at 60c on another.
- A picks the apple. B picks the donut.
- The change is worked out from whichever price was chosen.
How to use this: Click the green flag, then click the stage once. Type 3, then 2, then press A or B.
Teacher note
The trap is a pupil who gives each snack its own change variable. One price variable, set by whichever key was pressed, is the idea worth steering them to — it is the first time a variable holds “whatever the player chose”.
Not enough money
A real machine does not hand out a snack you have not paid for.
- If the money is less than the price, nothing drops.
- The machine says exactly how many cents more you need.
- If there is enough, it works as before.
How to use this: Click the green flag, then click the stage once. Type 1 then 0 to come up short; press the flag again and type 3 then 2 to buy it.
Teacher reveal
if <(money) < (price)> then
set [short by v] to ((price) - (money))
say (join [You need ] (join (short by) [c more.])) for (3) seconds
else
glide (0.6) secs to x: (-60) y: (-128)
say (join [Change ] ((money) - (price))) for (3) seconds
end
Both halves use the same two variables in the same subtract — the only difference is which way round. That is the whole point of the challenge, and it is worth saying out loud to the class.
A third coin
Add a 10c slot. Three piles of coins, one total.
- The machine asks for 20c, 50c and 10c coins.
- All three piles are added into
money. - The snack and the change still work.
How to use this: Click the green flag, then click the stage once. Type three numbers — try 3, then 2, then 5.
Teacher reveal — an add inside an add
set [money v] to (((twenties) * (20)) + (((fifties) * (50)) + ((tens) * (10))))
A + block has exactly two slots, so three piles cannot fit in one. The second slot holds another add. Pupils who cannot get it to nest usually have the right blocks in the wrong order on the canvas — tell them to build the inner add first, then drag the whole thing into the outer one.
Two snacks in one go
Buy a muffin and an apple together, and pay for both at once.
- A
basketvariable holds the two prices added together. - Both snacks glide to the tray, side by side.
- The change comes off the basket, not off one snack.
How to use this: Click the green flag, then click the stage once and type 3, then 2. Watch both snacks come down.
Teacher reveal
set [basket v] to ((45) + (30))
glide (0.6) secs to x: (-60) y: (-128)
broadcast [apple too v]
wait (0.8) seconds
set [change v] to ((money) - (basket))
say (join [Basket ] (join (basket) (join [c. Change ] (join (change) [c.])))) for (3) seconds
The apple’s own script is two blocks: when I receive [apple too v] and a glide to a spot beside the muffin. Prices typed as numbers here are fine for today; Lesson 3-5 puts them in a list.
Summary 5 min
(() * ())turns a count of coins into an amount of money.(() + ())puts two piles together;(() - ())is the change.- A slot can hold another operator block. That is nesting, and you read it from the inside out.
(answer)is one box that every ask overwrites — save it into a variable straight away.- Money minus price is the change. Price minus money is how short you are.
- Operator
- A green block that takes numbers in and reports one number back.
- Nesting
- Putting a block inside another block’s slot.
- Expression
- A sum built out of nested blocks, worked out from the inside out.
- Price
- What a thing costs — in this arc, a whole number of cents.
Hand in
Save your machine. Next lesson it stops saying “115c” and starts handing back real coins — which needs two blocks you have not met.