Learning Goals 3 min
By the end of this lesson you will be able to:
- Explain what the () mod () block reports — the remainder left over after dividing the first number by the second.
- Use round () to turn a messy decimal (like
3.7) into the nearest whole number. - Combine () mod () with () = () inside an if <> then to fire an event every Nth time — for example, a bonus pop-up every 5 points.
Warm-Up — sharing kuih equally 7 min
Aunty bought 10 pieces of kuih lapis for the cousins. There are 3 cousins. She wants to share equally, but she also wants to know how many pieces are left over for herself.
Two questions for you (don't peek at the answers yet):
- How many whole pieces does each cousin get?
- How many pieces are left over for Aunty?
Reveal the answer
Each cousin gets 3 pieces (because 3 × 3 = 9), and there is 1 piece left over for Aunty. In maths-speak: 10 ÷ 3 = 3 remainder 1. That "remainder 1" is exactly what () mod () reports. (10) mod (3) reports 1.
One more — Aniq is counting his steps to school. He counts 247 steps. He likes to chant a cheer every 10 steps. How can a script know exactly when the step count is a multiple of 10 — like 10, 20, 30, 240?
Reveal the answer
A number is a multiple of 10 when its remainder after dividing by 10 is zero. So (steps) mod (10) reports 0 at 10, 20, 30… and reports 1, 2, 3… in between. Combine it with () = () to test for that zero. You'll build exactly this in the worked example.
New Concept — remainders and rounding 15 min
The Operators palette has two small green blocks that most beginners ignore — until they need them. Once you see what they do, you spot uses everywhere.
Block 1 — () mod ()
"Mod" is short for modulo, a fancy word for remainder after division. The block has two round slots for numbers and reports a round number.
((10) mod (3))
1. Read it as: "ten divided by three, throw away the whole part, keep the leftover."A few more values to lock the idea in:
- (6) mod (3) reports 0 — 6 divides cleanly by 3, no leftover.
- (7) mod (3) reports 1 — 7 is one past a clean multiple of 3.
- (8) mod (3) reports 2 — 8 is two past.
- (9) mod (3) reports 0 again — and the pattern repeats: 0, 1, 2, 0, 1, 2, 0…
That repeating 0, 1, 2… pattern is the magic. Mod gives you a counter that resets on its own.
Why this matters — every-Nth events
The most common Scratch use of mod is the every-Nth-time trick. Put mod inside a comparison, put the comparison inside an if-then:
if <((score) mod (5)) = (0)> then
say [Bonus!] for (1) seconds
end
Block 2 — round ()
The other block today is even smaller. round () takes one round slot and reports the nearest whole number.
(round (3.7))
4. round (3.2) reports 3. round (3.5) reports 4 (Scratch rounds halves up).Why would you need this? Because division and square root and distance to all give you messy decimals like 17.3849. If you want to show that as a score on screen, a giant decimal looks ugly. Wrap it in round and you get a clean 17.
set [tens-away v] to (round ((distance to [Sprite1 v]) / (10)))
17 — "about 17 tens away". Much nicer than 17.3849.Worked Example — adding an even/odd bonus to the Lucky Number Machine 12 min
We upgrade the Lucky Number Machine from Lesson 3. Two new things: mod adds a 3-point bonus when the total is even; round makes the average score display a clean whole number. Eight steps.
total mod 2 = 0. The average score panel (right) shows a rounded whole number. The cat (✏️) runs the whole updated script.Step 1 — Open the Lucky Number Machine project
Open the project from Lesson 3. The cat has the random dice rolls and the total calculation from last time.
Step 2 — Make the variable and reset the score
Make a new variable score. At the top of the flag script, add set [score v] to (0) before the dice rolls. We'll add to score based on the total.
Step 3 — Set score to the total
After computing total, add: set [score v] to (total). The score starts equal to the total.
Step 4 — Add the even-number bonus using mod
if <((total) mod (2)) = (0)> then
change [score v] by (3)
say [Even bonus! +3] for (1) seconds
end
Step 5 — Make an average score variable
Make a variable avgScore. Set it to ((total) + (score)) / (2) — the average of the raw total and the bonus score. This will be a decimal for most rolls.
Step 6 — Display the average rounded
After setting avgScore, wrap it in round when you say it:
say (join [Avg: ] (round (avgScore))) for (2) seconds
Step 7 — Full assembled flag script
when flag clicked
set [score v] to (0)
set [die1 v] to (pick random (1) to (6))
set [die2 v] to (pick random (1) to (6))
set [total v] to ((die1) + (die2))
set [score v] to (total)
if <((total) mod (2)) = (0)> then
change [score v] by (3)
say [Even bonus! +3] for (1) seconds
end
set [avgScore v] to (((total) + (score)) / (2))
say (join [Score: ] (score)) for (2) seconds
say (join [Avg: ] (round (avgScore))) for (2) seconds
Step 8 — Test it
Click the flag repeatedly. On even totals (2, 4, 6, 8, 10, 12) the cat shouts "Even bonus! +3" and the score jumps. On odd totals the bonus is silent. The average always shows a clean whole number. Scoring is now fair and tidy.
Try It Yourself — three drills 15 min
Goal: Make a sprite that says the rounded version of its own y-position whenever you click it. Drag the sprite around the Stage first, then click — the bubble should show a clean whole number like 87, not 86.7421.
when this sprite clicked
say (round (y position)) for (1) seconds
Think: Without round (), dragging the sprite a tiny bit gives weird decimals because the mouse never lands on perfect whole-number pixels. Round is the polish.
Goal: Build a stopwatch sprite that ticks once per second using wait (1) seconds, and plays a play sound [pop v] only on even seconds (2, 4, 6…). Use mod-equals-zero with 2.
when flag clicked
set [tick v] to (0)
forever
wait (1) seconds
change [tick v] by (1)
if <((tick) mod (2)) = (0)> then
play sound [pop v]
end
end
Think: Change 2 to 3 and the pop plays every third tick. Mod's right slot is the "every how many?" dial.
Goal: Make the cat alternate between two costumes by itself. Every odd score, costume "costume1". Every even score, costume "costume2". (Hint: (score) mod (2) reports 1 for odd, 0 for even.) Use if <> then else.
when this sprite clicked
change [score v] by (1)
if <((score) mod (2)) = (0)> then
switch costume to [costume2 v]
else
switch costume to [costume1 v]
end
Think: You just built a toggle — flip-flop behaviour from one mod block. This pattern is how blinkers, on/off switches, and zebra-stripe backgrounds get built.
Mini-Challenge — Mei's never-shouting cat 5 min
"Why won't it cheer?"
Mei copied the worked-example pattern but tweaked it for her own game. She wants the cat to shout "Hooray!" every 3 clicks. She clicks 3, 6, 9, 12 times — and nothing happens. Here's her stack:
when this sprite clicked
change [score v] by (1)
if <((score) mod (3)) = (1)> then
say [Hooray!] for (1) seconds
end
What's actually happening, and how would you fix it?
Reveal one valid solution
Mei is comparing (score) mod (3) to 1, not 0. So the cheer fires when the score is 1, 4, 7, 10, 13… — one after every multiple of 3, not on them. The remainder is 0 exactly when score is a clean multiple of 3.
Fix: change the 1 to 0:
when this sprite clicked
change [score v] by (1)
if <((score) mod (3)) = (0)> then
say [Hooray!] for (1) seconds
end
Now the cheer fires at score 3, 6, 9, 12 — the multiples of 3. Lesson: for "every Nth", always compare mod to 0. Comparing to any other number gives you the slots between the multiples.
Recap 3 min
You met two small green Operators that punch above their weight. () mod () reports the remainder after dividing — and when you compare that remainder to zero, you've built an "every Nth time" detector for milestones, blinkers, alternating costumes, and rhythm. For the Lucky Number Machine, mod-equals-zero detects even totals and awards a bonus. round () trims an ugly decimal down to the nearest whole number, keeping the average score display tidy. Both blocks are tiny in size and huge in usefulness.
- Modulo (mod)
- The leftover after one number is divided by another. (10) mod (3) reports
1because 10 ÷ 3 is 3 with 1 left over. - Multiple
- A number you get by multiplying another whole number. Multiples of 5 are 0, 5, 10, 15, 20… A number is a multiple of N when
(it) mod (N) = 0. - Remainder
- The "left over" part of a division that doesn't go in evenly. The output of () mod ().
- Round
- To turn a decimal into the nearest whole number. round (3.7) reports
4; round (3.2) reports3. - Milestone
- A specific score, time, or count worth celebrating. Mod-equals-zero is the standard way to detect them inside a forever loop.
Homework 2 min
The Every-Third-Click Game. Build a one-sprite project that turns clicks into a tiny rhythm.
- One sprite (any costume), one variable
clicks, set to 0 on flag. - When the sprite is clicked, change
clicksby 1. - On every 3rd click, play play sound [pop v].
- On every 10th click, also say say [Score milestone!] for (1) seconds.
- Add one more sprite anywhere on the Stage that continuously shows say (round ((clicks) / (3))) — "how many full sets of 3 have I clicked?". Use forever + say (the watcher version, no time).
Save as HW-L3-04-Mod-And-Round.sb3. Click your cat 30 times in a row — you should hear 10 pops, see 3 "milestone" speech bubbles, and the second sprite should end up showing 10.
Bring back next class:
- The
.sb3file. - Your answer to: "At click number 9, what does ((clicks) mod (3)) report, and what does (round ((clicks) / (3))) report? Explain in one sentence each."
Heads up for next class: SCR-L03-05 meets the compound [abs v] of () block. We'll use abs to measure exactly how far the Lucky Number Machine's total lands from the target — and floor to give the final polished score before we ship the game.