- 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 — and what is awkward about the answer?
Reveal the answer
40.5c, and there is no half-cent coin. Today is about what a machine does when the maths lands between two coins.
Today’s Topic 3 min
- A percentage off, and the number it leaves behind
(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. Correct, and impossible: 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 and 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, and 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 and 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, and the others are there whenever 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. What you need is to know that one block holds all of them, so when a project needs a square root you go looking in a dropdown rather than 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 and compares it with what the sums say should be there. The difference matters; which way it went does not, 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: how big a number is, ignoring whether it is above or below zero.
How to use this: Click the green flag and watch three tills checked in turn: too much, too little, and exactly right.
Basic — discount, change and a till 24 min
Open your machine. Three things to add, 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 and 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
takingsand 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 around 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 being worked out from price and the money is being taken from to pay, or 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 as well as 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, and 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 and 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, and the machine finds the day it was furthest out.
- Each day the till is a little above or a little 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 and 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 and 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 short-changes anybody — and that 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 generosity 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 decision, 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, price a snack, hand back real change and close its till at the end of the day.
Next lesson starts a new arc, and a new kind of box: one variable that can hold a whole shelf of prices at once.