Learning Goals 3 min
By the end of this lesson you will be able to:
- Use add [thing] to [shopping v] to append an item to the end of a list — text or numbers.
- Combine ask [What to add?] and wait with (answer) to build a "type, hit enter, it joins the list" mini-app.
- Reset a list cleanly at the start of every run with delete all of [shopping v] so old items don't pile up forever.
Warm-Up — the list that never empties 7 min
Last lesson you typed items into your shopping list by hand using the + button. Today the script will do the typing. But first, a question.
Imagine you make a list called visitors and add the name Aiman by hand. You close Scratch, go for lunch, come back, and re-open the project. Click the green flag. Then you add the name Hafiz by hand. How many items are in the list now?
Reveal the answer
Two — Aiman (from before lunch) and Hafiz (from after). Lists are saved with the project, just like variables. Closing Scratch doesn't empty them. Clicking the green flag doesn't empty them either. They keep their items until you explicitly delete them. This is great for things you want to remember (a high score table) and terrible for things you want fresh every time (a quiz that should start blank). Today's lesson covers both — adding items, and clearing them out.
One more thing before we start. Open HW-L3-07-Three-Lists.sb3 from last week. Click the green flag a few times. Did your sayur list grow? No — because you never added items in a script. By the end of this lesson, you'll have a list that grows every time someone types.
New Concept — the add block 15 min
Once a list exists, the Variables palette shows a whole new section of blocks below the Make-a-List button. The most important one — the one you'll use in 90% of list scripts — is right at the top.
Where to find it
Open Variables. Scroll past the Make buttons. The first list block you see is:
add [thing] to [shopping v]
Whatever you type (or drop) into the square slot becomes a brand new item at the end of the list. The list grows by one. The new item gets the next index number — if the list had 3 items, the new one is item 4.
Append means end
The add block always goes to the bottom of the notepad. There's no "add at the top" version of this block (we'll meet insert in a later lesson). For now, every new thing lands at the end of the list, like a queue at the canteen.
when flag clicked
add [kangkung] to [shopping v]
add [tauhu] to [shopping v]
add [cili] to [shopping v]
kangkung, tauhu, cili. Click it again: list is kangkung, tauhu, cili, kangkung, tauhu, cili. Things pile up.The reset block — delete all of
To stop the pile-up, put delete all of [shopping v] at the very top of the script — before any add blocks. It empties the list, length goes back to 0, then the add blocks fill it again from scratch.
when flag clicked
delete all of [shopping v]
add [kangkung] to [shopping v]
add [tauhu] to [shopping v]
add [cili] to [shopping v]
Numbers count too
Scratch lists don't care about the type of thing inside them. Text, numbers, decimals, even one-letter strings — all welcome.
when flag clicked
delete all of [scores v]
add (10) to [scores v]
add (25) to [scores v]
add [bonus!] to [scores v]
add (7.5) to [scores v]
Asking the user — the killer combo
The real magic of add happens when you don't know what to add until the user tells you. The Sensing block ask () and wait pops up a text box on the Stage. Whatever the user types lands in the special block (answer). Drop (answer) into the slot of the add block and you've built a "type and add" mini-app.
when flag clicked
delete all of [shopping v]
forever
ask [What to add?] and wait
add (answer) to [shopping v]
end
roti, hit enter. roti appears in the list watcher. The box reappears. Type susu, hit enter. And so on — forever. The list grows with every entry.Worked Example — a build-your-own pasar list 12 min
Open Scratch. We'll build a project where you type each item and it appears in the list automatically.
shopping watcher, top-left. The cat (the ✏️ sprite you script) runs the ask-and-add loop. Find and Sort buttons come in later lessons.Step 1 — Start fresh
New project. Default cat. Make a list called shopping, For all sprites (lesson L03-07 if you've forgotten how). The empty watcher appears on the Stage.
Step 2 — Drop the hat
From Events: when ⚑ clicked.
Step 3 — Reset the list
From Variables: delete all of [shopping v]. Snap it under the hat. This makes sure every run starts with an empty list.
Step 4 — Add the forever loop
From Control: forever. Snap it under the delete block. The C-shape opens, ready for the question-and-add pair.
Step 5 — Ask the question
From Sensing: ask [What to add?] and wait. Drop it inside the forever. Change the default text from What's your name? to What to add? by clicking the white box.
Step 6 — Add the answer to the list
From Variables: add [thing] to [shopping v]. Drop it inside the forever, below the ask block. From Sensing: (answer). Drag it onto the square slot of the add block. The word thing is replaced by the round answer reporter.
Step 7 — Click the flag and try it
A text box appears at the bottom of the Stage. Type roti. Hit enter. roti appears at the bottom of the shopping watcher. The text box pops up again. Type susu. Hit enter. susu appears below roti. Keep going.
Step 8 — Click the flag again
The list empties (thanks to the delete-all at the top) and you start fresh. Without that delete-all, the new items would pile up on top of the old ones — try it once with the delete block removed, just to see the bug, then put it back.
The full assembled stack
when flag clicked
delete all of [shopping v]
forever
ask [What to add?] and wait
add (answer) to [shopping v]
end
What you just built: the exact same input pattern that every to-do app, chat app, notes app and quiz-maker uses — ask for input, append to a list, repeat. Everything from a homework tracker to a guest list at your birthday kenduri is this five-block stack with a different word in the ask block.
Try It Yourself — three add-to-list drills 15 min
Goal: Make a list called kawan. Build a script that, when the flag is clicked, empties the list and then adds three friends' names by hand-typing them into three add blocks.
when flag clicked
delete all of [kawan v]
add [Aiman] to [kawan v]
add [Mei Ling] to [kawan v]
add [Priya] to [kawan v]
Think: Notice the order in the watcher matches the order of the add blocks. Top-to-bottom in the script = top-to-bottom in the list. That's not an accident — Scratch runs blocks in order.
Goal: Make a list called scores. Build a "type a score" script with ask () and wait and add (answer) to [scores v] inside a forever loop. Reset the list at the top. Test by typing 10, 25, 17 — three numbers.
when flag clicked
delete all of [scores v]
forever
ask [Score?] and wait
add (answer) to [scores v]
end
Think: Did you notice you can type 20 and the list shows 20 with no quotes? Try typing abc next — Scratch happily adds it too. Lists are flexible. Whether that's good or bad depends on your project.
Goal: Build a "guest book" project. Make a list called visitors. On the green flag, empty it. Then ask Your name? and add the answer. But don't loop forever — only loop 5 times (use repeat (5)). After 5 names, the cat says Senarai penuh! for 2 seconds.
when flag clicked
delete all of [visitors v]
repeat (5)
ask [Your name?] and wait
add (answer) to [visitors v]
end
say [Senarai penuh!] for (2) seconds
Think: You're meeting a fixed-count loop. forever never lets the script move past it; repeat (5) finishes and the script continues. Notice the say-block runs only after the 5th name — because Scratch waits for the repeat to finish.
Mini-Challenge — the duplicate visitor 5 min
"Nadia's growing guest list"
Nadia is building a guest list for her birthday kenduri. She wants every name typed to appear in the jemputan list. She writes this:
when flag clicked
forever
ask [Who's coming?] and wait
add [answer] to [jemputan v]
end
Aunty Siti, hits enter, and looks at the list watcher. Something is very wrong.Mentally click the flag and type Aunty Siti. What does the list watcher actually show, and why?
Reveal one valid solution
The list shows the literal word answer. Five times in a row if Nadia types five names. The bug is in the add block: Nadia wrote [answer] with square brackets, which means she typed the word "answer" into the square slot — instead of dragging the (answer) reporter from the Sensing palette into that slot.
Square slot + typed text = literal string. Square slot + dropped reporter = whatever the reporter says. Nadia did the first when she meant the second.
when flag clicked
delete all of [jemputan v]
forever
ask [Who's coming?] and wait
add (answer) to [jemputan v]
end
Notice the round brackets around answer in the fixed version — that's how scratchblocks shows a dropped reporter. Also: a delete all at the top so re-runs start fresh. Square brackets = you typed it. Round brackets = a reporter block was dropped.
Recap 3 min
You met the most-used list block in Scratch. add [thing] to [shopping v] appends one item to the end of a list — the list grows by one and the new item gets the next index number. Pair it with ask () and wait + (answer) inside a forever loop and you have a complete "type and add" mini-app — the same pattern every to-do, chat and quiz app uses. Always start with delete all of [shopping v] if you want a fresh list each run; otherwise items pile up forever. Numbers and text are both welcome. Next lesson we stop just adding and start reading items back.
- Append
- Adding a new item to the end of a list. The add block appends — there's no "add to top" version of it.
- Index
- The position number of an item in a list, starting at 1 (not 0). The first item is index 1, the second is index 2, and so on. The newest appended item has the largest index.
- delete all of
- delete all of [shopping v] empties a list in one go. Length becomes 0. The list itself still exists — just with no items.
- (answer)
- A round reporter in the Sensing palette that holds whatever the user most recently typed into an ask () and wait box. Update happens the moment the user hits enter.
Homework 2 min
The Class Roster. Build one project that collects a list of classmates and a parallel list of their favourite foods.
- Make two lists:
namesandfoods(both For all sprites). - On the green flag, empty both lists with two delete all of blocks.
- Use a repeat (5) loop. Inside it: ask
Nama?, add the answer tonames, then askMakanan kegemaran?, add the answer tofoods. - After the repeat, the cat says
Roster siap!for 2 seconds.
Save as HW-L3-08-Roster.sb3. Run it and type 5 real classmates with 5 real foods. Both list watchers should fill up side by side.
Bring back next class:
- The
.sb3file with both lists filled. - Your answer to: "After the repeat finishes, look at
namesitem 3 andfoodsitem 3. Do they belong to the same person? Why or why not?"
Heads up for next class: SCR-L03-09 teaches item (1) of [shopping v] — the block that reads one specific item back out. We'll meet the rule that list indexes start at 1, not 0, and we'll meet the last and random shortcuts. The homework you just built is the perfect dataset to read from next class.