Learning Goals 3 min
By the end of this lesson you will be able to:
- Drop the round reporter length of [shopping v] into a script and explain that it always reports a number — how many items the list currently has.
- Combine it with a comparison operator to ask yes/no questions like <(length of [shopping v]) = (0)> (is the list empty?) and <(length of [shopping v]) > (10)> (has the list got too many things?).
- Use (length of [shopping v]) + (1) to work out the index of the next item — useful for numbering things in a say message.
Warm-Up — how many things are on Hafiz's list? 7 min
Hafiz is keeping a list called shopping for his mum's pasar malam trip. He started the day with three items, then added two more, then deleted one because the kedai didn't have it. He now wants Scratch to say out loud how many things are still on the list — but he doesn't want to count by hand every time.
when flag clicked
add [nasi lemak] to [shopping v]
add [roti canai] to [shopping v]
add [teh tarik] to [shopping v]
add [kuih lapis] to [shopping v]
add [pisang goreng] to [shopping v]
delete (3) of [shopping v]
say [How many items are left?] for (2) seconds
Hafiz could count in his head: 5 added, 1 deleted, so 4 left. But what if the script ran 100 times with different items? Predict — is there a single block that just reports the current count, no matter what?
Reveal the answer
Yes. Scroll down inside the orange Variables palette to the list-related blocks. There's a round reporter called length of [shopping v]. After Hafiz's script runs, that reporter shows the number 4 — automatically. No counting in your head. Today's lesson is that one block, and the surprising number of useful things it unlocks.
One block. One number. We'll spend the whole lesson on what that number lets you do.
New Concept — the length reporter 15 min
You already know add, delete, and the round item reporter from earlier lessons. Today's block is one of the simplest in the whole palette — and one of the most useful.
Where to find it
Open the orange Variables palette. Scroll down past the variable blocks until you reach the list section. Make sure your shopping list already exists (Make a List → name it shopping). You'll see a row of list reporters — one of them is round and looks like this:
(length of [shopping v])
Tick its checkbox to show the value on the Stage. Add a few items, delete one — the number on the Stage updates instantly.
It always reports a number — even when the list is empty
If your list has no items, length of [shopping v] reports 0. If it has one item, it reports 1. If it has 50, it reports 50. It's never blank, never an error, never "no value" — it's always a whole number, starting at zero.
Drop it into a say block
Because it's round, it fits in any round slot. The easiest way to see it is to feed it to say () for (2) seconds:
when flag clicked
say (length of [shopping v]) for (2) seconds
4.Drop it into a question — that's where it gets powerful
The real magic is feeding length into a comparison operator (those green hexagonal blocks from L02-08). Now you can ask the list real questions:
if <(length of [shopping v]) = (0)> then
say [Your list is empty, lah!] for (2) seconds
end
if <(length of [shopping v]) > (10)> then
say [Too many things! Kedai run will be long.] for (2) seconds
end
One more trick — the next-index pattern
If your list currently has 4 items, what number does the next item get when you add it? Answer: 5. In general, the next index is (length of [shopping v]) + (1). That sounds small, but it's the foundation of numbered output:
say (join [Adding item number ] ((length of [shopping v]) + (1))) for (2) seconds
add [kuih lapis] to [shopping v]
Adding item number 5 when the list already has 4.Worked Example — Mei Ling's tuckshop counter 12 min
Mei Ling helps at the school tuckshop. She wants a Scratch project that adds drinks one at a time, shows the running count, and warns her if the tray gets too full (more than 8 drinks). Let's build it in eight steps.
drinks watcher top-left, with the live length of count ticked on below it. The cat (the ✏️ sprite) reports the running total after every add. Same counting trick will soon tell Mak how many pasar items are left.Step 1 — Start fresh
New Scratch project. Default cat. From Variables, click Make a List and name it drinks. Tick its checkbox so the list shows on the Stage. Also make sure length of [drinks v] is ticked.
Step 2 — Empty the list at flag click
From Events: when ⚑ clicked. From Variables: delete all of [drinks v]. This guarantees a clean start every run.
Step 3 — Ask the user for a drink name
From Sensing: ask [What drink? (or 'done')] and wait. The user types something like milo ais and presses Enter.
Step 4 — Loop until they type 'done'
From Control: repeat until <>. Drop <(answer) = [done]> into the diamond. This is L02-08 comparison-operator stuff — it stops as soon as the user types done.
Step 5 — Inside the loop, add the answer to the list
From Variables: add (answer) to [drinks v]. Drop it inside the repeat-until.
Step 6 — Show the running count after each add
From Looks: say () for (1) seconds. Inside its slot, drop join [Tray now has ] (length of [drinks v]). So after each add, the cat reports the new count.
Step 7 — If the tray gets too full, warn her
Still inside the loop, after the say block, add if <> then with <(length of [drinks v]) > (8)> in the diamond, and say [Tray full! Stop adding!] for (2) seconds inside.
Step 8 — Ask again, so the loop can continue
Last block inside the loop: another ask [What drink? (or 'done')] and wait. Without this, the loop would check the same old answer forever.
The full assembled stack
when flag clicked
delete all of [drinks v]
ask [What drink? (or 'done')] and wait
repeat until <(answer) = [done]>
add (answer) to [drinks v]
say (join [Tray now has ] (length of [drinks v])) for (1) seconds
if <(length of [drinks v]) > (8)> then
say [Tray full! Stop adding!] for (2) seconds
end
ask [What drink? (or 'done')] and wait
end
say [Final count:] for (1) seconds
say (length of [drinks v]) for (2) seconds
What you just built: the same loop-and-count pattern shopkeepers everywhere use — collect input until "done", show the total. The length of [drinks v] reporter did all the counting for you. You never had to maintain a separate "count" variable.
Try It Yourself — three length drills 15 min
Goal: Make a list called friends. Add five names by hand inside the script. After the adds, make the cat say the current length out loud for 2 seconds.
when flag clicked
delete all of [friends v]
add [Aisha] to [friends v]
add [Boon] to [friends v]
add [Chong] to [friends v]
add [Devi] to [friends v]
add [Ezra] to [friends v]
say (length of [friends v]) for (2) seconds
5.Think: If you delete one of the adds and run again, the cat should now say 4 — without you changing the say block. That's the whole point of using the reporter instead of typing the number.
Goal: Build a list called tasks. Use ask + repeat (5) to let the user add five tasks. After the loop, only if the list has more than 3 items, the cat should say You're busy today!.
when flag clicked
delete all of [tasks v]
repeat (5)
ask [Type a task:] and wait
add (answer) to [tasks v]
end
if <(length of [tasks v]) > (3)> then
say [You're busy today!] for (2) seconds
end
3 and the warning should never fire.Think: The length check happens after the loop — so it sees the final count, not the count partway through. Where you put the if-then matters a lot.
Goal: Build a guest-list project for a birthday party. The cat asks "Who's coming?" over and over. After each answer, the cat reports Guest number N added: NAME, using the next-index pattern. Stop when the user types done.
when flag clicked
delete all of [guests v]
ask [Who's coming? (or 'done')] and wait
repeat until <(answer) = [done]>
say (join [Guest number ] ((length of [guests v]) + (1))) for (1) seconds
add (answer) to [guests v]
ask [Who's coming? (or 'done')] and wait
end
say (join [Total guests: ] (length of [guests v])) for (2) seconds
Think: Notice the say happens before the add — so we're reporting the next index, which is current length + 1. If we put the say after the add, we'd use just length of [guests v] with no +1. Either is fine; you just have to be consistent.
Mini-Challenge — Priya's empty-list mistake 5 min
"The shop that opens with nothing"
Priya wants her game to warn the player "Cart is empty!" whenever the player tries to checkout with no items. She writes this:
when [checkout v] key pressed
if <(length of [cart v]) = (1)> then
say [Cart is empty!] for (2) seconds
end
Think about what empty actually means in terms of the length number.
Reveal one valid solution
Priya wrote = 1, but an empty list has length zero, not one. A list with one item has length 1 — that's not empty, it has a thing in it. The fix is to change the 1 to a 0:
when [checkout v] key pressed
if <(length of [cart v]) = (0)> then
say [Cart is empty!] for (2) seconds
end
Same three blocks. One number changed. Empty-list checks always compare to 0, because that's what "no items" means in the world of length. Memorise this: empty list = length 0.
Recap 3 min
You met your fourth list block: the round reporter length of [shopping v]. It reports one thing — a number — and that number is always the current count of items in the list. By itself it's not exciting; combined with comparison operators it lets you ask the list real questions like "are you empty?" or "are you too full?". Combined with arithmetic, it gives you the next-index pattern. The length reporter is the small block that turns a passive list into a list your script can reason about.
- Length
- The number of items currently in a list. An empty list has length 0. The reporter length of [shopping v] always reports this number.
- Round reporter
- An oval-shaped block that reports a value (usually a number or text). Fits in any matching oval slot. length of [shopping v] is one; x position is another.
- Empty-list check
- The conditional pattern <(length of [list v]) = (0)>. The standard way to ask "does this list have nothing in it?" — every list-using program needs this at some point.
- Next-index pattern
- The arithmetic (length of [list v]) + (1). Reports the slot number that the next add will use. Handy for numbered messages like "Adding item 5".
- Comparison operator
- A green hexagonal Operators block — () = (), () > (), () < (). Takes two values, reports true or false. Length feeds into these to make list-questions.
Homework 2 min
The Pasar Malam Tracker. Build one Scratch project that tracks items added to a list called cart, with three length-based behaviours.
- At flag click: delete all of [cart v] so each run starts fresh.
- Use ask + repeat until <> to let the user add items until they type
done. After each add, the cat says the new length: say (join [Items: ] (length of [cart v])) for (1) seconds. - After the loop, three length-based reactions: if length is 0, say
You bought nothing, aiyo!; if length is between 1 and 5, sayNice small trip.; if length is more than 5, sayWah, big shopping today!. (For the middle one, use two stacked if-thens — or use and from L02-10.)
Save as HW-L3-10-Pasar-Malam-Tracker.sb3. Hit the flag and try three runs: one with 0 items, one with 3, one with 8. All three messages should fire correctly.
Bring back next class:
- The
.sb3file. - Your answer to: "Why is checking
= 0the same as 'is the list empty?' but checking= 1is not the same as 'has one item'?" (Hint: think trick question —= 1actually is "has exactly one item". Write what's different about the empty case.)
Heads up for next class: SCR-L03-11 meets the three list-mutation blocks beyond add: replace, insert, and delete. You'll be able to edit any slot in a list, not just push to the end.