- Learnt28 min · round, floor, ceiling, abs
- Basic24 min · discount, change and a till
- Challenge 1–327 min · half price, out by, the report
- Extrabonus · round the customer’s way
Review — where we got to 5 min
([floor v] of ())and(() mod ())break change into coins.(pick random (1) to (6))makes a machine different every morning.- Roll once into a variable. Then use the variable everywhere.
Quick-fire
Your machine takes 10% off a 45c muffin. What should the customer pay? What is awkward about the answer?
Reveal the answer
40.5c, and there is no half-cent coin. Today we learn what a machine does when the maths lands between two coins.
Today’s Topic 3 min
- A percentage off, and the number it leaves
(round ())— to the nearest whole number([floor v] of ())and([ceiling v] of ())— always down, always up([abs v] of ())— how far out, without the minus sign
Learning outcome
By the end of this lesson you will be able to:
- Work out a percentage of a price.
- Choose between round, floor and ceiling, and say why.
- Use abs to measure a difference in either direction.
Learnt — tidying a number up 28 min
1 · Ten per cent off
Per cent means out of a hundred. Taking 10% off means the customer pays 90 out of every 100. So multiply by 90 and divide by 100:
set [exact v] to (((price) * (90)) / (100))
For a 45c muffin that is 40.5. It is correct, but no machine can take half a cent.
2 · Round goes to the nearest
set [to pay v] to (round (exact))
How to use this: Click the green flag. Read the three boxes as it talks through the sum.
3 · Floor, ceiling and round, side by side
You met floor last lesson. It has two neighbours in the same dropdown. The three of them do different jobs:
| Block | What it does | 40.5 becomes |
|---|---|---|
(round ()) | Nearest whole number | 41 |
([floor v] of ()) | Always down. The customer wins. | 40 |
([ceiling v] of ()) | Always up. The shop wins. | 41 |
How to use this: Click the green flag. Watch five random numbers go through all three blocks. Read the four boxes together.
4 · The rest of the dropdown
Open that dropdown again and look down the list. Floor, ceiling and abs are three of about a dozen. The others are there when you need them:
([sqrt v] of (81))
([sin v] of (90))
There is also cos, tan, ln, log, e ^ and 10 ^. You do not need them today. Just know that one block holds all of them. So when a project needs a square root, look in this dropdown. Do not hunt for a block that does not exist.
5 · The till, and abs
At the end of the day, a shop counts the money in the till. It checks it against what the sums say should be there. The difference matters. Which way it went does not matter, not at first:
set [out by v] to ((takings) - (expected))
set [out by v] to ([abs v] of ((takings) - (expected)))
Abs is short for absolute value. It is how big a number is. It does not care if the number is above or below zero.
How to use this: Click the green flag. Watch three tills checked in turn: too much, too little, and exactly right.
Basic — discount, change and a till 24 min
Open your machine. Add three things, and then it is finished.
Step 1 — today’s discount
- Make two variables:
exactandto pay. - After the price is rolled, add
set [exact v] to (((price) * (90)) / (100)). - Then
set [to pay v] to (round (exact)). - Show both boxes. Press the flag a few times.
Step 2 — charge the discounted price
- Everywhere your machine used
price, useto payinstead. - Check the change ladder still adds up.
Step 3 — the till
- Make a variable called
takings. Set it to 0 under the green flag. - After every sale, add
change [takings v] by (to pay). - Show the box and buy three things. Check the total by hand.
Step 4 — tell the customer
- Say the discounted price before they choose.
- Say the change after they buy.
- Say the till total last.
when flag clicked
set [takings v] to (0)
set [price v] to (45)
set [exact v] to (((price) * (90)) / (100))
set [to pay v] to (round (exact))
say (join [10% off today: ] (join (to pay) [c. Press A.])) for (3) seconds
when [a v] key pressed
set [change v] to ((money) - (to pay))
change [takings v] by (to pay)
glide (0.6) secs to x: (-60) y: (-128)
say (join [Change ] (join (change) [c.])) for (2) seconds
say (join [Till: ] (join (takings) [c.])) for (3) seconds
How to use this: Click the green flag. Then click the stage once. Type 3, then 2, then press A.
The to-pay box shows something like 40.50000000000001.
You have rounded the wrong thing, or not rounded at all. The round goes around the whole sum, not just the price.
The till total is bigger than everything I have sold.
set [takings v] to (0) is missing under the flag. So the total carries over from the last run.
The change is wrong by one cent.
The change is worked out from price, but the money is taken from to pay. Or it is the other way round. Pick one and use it in both places.
The discount is applied twice.
The set [to pay v] to () is inside the key script, and also under the flag. So it runs again on every sale. 90% of 90% is 81%.
Half price day
Change the offer from 10% off to half price. See how often it lands badly.
- The price is rolled, then halved.
- The exact half and the rounded price are both shown.
- Run it five times and count how many needed rounding.
How to use this: Click the green flag. Watch five prices get halved. Read the exact box each time.
Teacher note
The pattern is worth drawing out: a price that is a multiple of 10 halves cleanly; one that is a multiple of 5 but not 10 does not. That is ((price) mod (10)) from last lesson, being useful again.
The worst day
Six days of takings. The machine finds the day it was furthest out.
- Each day the till is a little above or below what was expected.
- A
worstvariable keeps the biggest gap so far. - It does not matter whether the till was over or under.
How to use this: Click the green flag. Watch six days go past. Press it again for a different worst day.
Teacher reveal — keeping the biggest so far
set [out by v] to ([abs v] of ((takings) - (expected)))
if <(out by) > (worst)> then
set [worst v] to (out by)
end
Without abs the worst box would stay stuck on whichever day was most over, and a day 30c short would never beat it. This "keep the biggest so far" shape is the same one Level 2 used for a high score — and it is one step from the bubble sort in Lesson 3-11.
The end-of-day report
Close the machine down properly. One report, everything in it.
- How many snacks were sold.
- How much is in the till.
- Whether the till balances, and by how much if not.
How to use this: Click the green flag. Watch five sales, then the report at the end.
Teacher reveal
set [expected v] to ((sold) * (to pay))
set [out by v] to ([abs v] of ((takings) - (expected)))
say (join (sold) (join [ sold, ] (join (takings) [c taken.]))) for (2.5) seconds
say (join [Till out by ] (join (out by) [c. Closing up.])) for (3) seconds
Ask the class to break it on purpose: add 5c to the takings by hand and watch the report catch it. A report that cannot ever say anything is wrong is not a report.
Round the customer’s way
Build a machine that never gives anybody too little change. It also knows exactly what that kindness costs.
- The price is floored. So the customer never pays the extra half cent.
- The change is ceilinged. So they never get a half cent less back.
- A
given awayvariable adds up everything the shop handed over.
How to use this: Click the green flag and watch five sales. The last bubble says what the kindness cost.
Teacher reveal
set [fair pay v] to ([floor v] of (exact))
set [fair change v] to ([ceiling v] of (exact change))
change [given away v] by (((exact) - (fair pay)) + ((fair change) - (exact change)))
⚠️ The total will show something like 5.400000000000001. That is not a bug in the pupil’s code — it is how computers store fractions, and it is worth two minutes of the lesson. The fix is the same trick as rounding money anywhere: ((round ((given away) * (10))) / (10)).
Summary 5 min
- A percentage is a multiply and a divide. 10% off is × 90 ÷ 100.
(round ())goes to the nearest whole number.([floor v] of ())always goes down;([ceiling v] of ())always goes up.([abs v] of ())is how far from zero, with no minus sign.- Which way you round is a choice, not a rule. And somebody pays for it.
- One dropdown holds floor, ceiling, abs,
sqrt,sinand the rest. Go looking in it before deciding a block does not exist.
- Per cent
- Out of a hundred. 10% of 45 is 4.5.
- Round
- Move to the nearest whole number.
- Ceiling
- The whole number above. Ceiling of 2.1 is 3.
- Absolute value
- How big a number is, ignoring its sign. Abs of −3 is 3.
Hand in
Share your finished machine and send the link to your teacher. It should take coins and price a snack. It should hand back real change. It should close its till at the end of the day.
Next lesson starts a new arc, and a new kind of box. It is one variable that can hold a whole shelf of prices at once.