Learning Goals 3 min
By the end of this lesson you will be able to:
- Create two list data structures — questions and answers — and explain why parallel lists beat one long string of "Q1: ... A1: ...".
- Populate both lists on green flag using add [ ] to [ v], keeping items at the same index aligned across the two lists.
- Ask the player a question with ask ( ) and wait using item ( ) of [questions v], then compare their reply against item ( ) of [answers v] with the same index.
Warm-Up — predict the messy single-string approach 7 min
This is the start of a five-lesson cluster on building a full trivia quiz studio. Today's lesson is the foundation: where do the questions live?
Here's a first attempt — a single variable called qa that stores everything as one long sentence:
when flag clicked
set [qa v] to [Q1: Capital of Malaysia? A1: Kuala Lumpur Q2: ...]
ask [What is the capital?] and wait
If your teacher hands you ten more trivia questions tomorrow, how easy is it to add them? And if the game wants to pick question 3, how does it know where Q3 starts and ends?
Reveal the answer
Adding questions means editing the giant string by hand — you have to find the right spot, type the new "Q11: ... A11: ..." in the middle of a wall of text, and hope you didn't miss a space. Picking question 3 means asking Scratch to slice the string at exactly the right characters, which Scratch can't easily do. There's no structure — just one blob.
The fix is two parallel lists. Item 1 of questions is the prompt; item 1 of answers is the matching reply. Add a question? One click on each list. Pick question 3? item (3) of [questions v]. That's today.
New Concept — parallel lists 15 min
A list in Scratch is a numbered stack of items. You make one in the Variables palette (scroll down, click Make a List). Lists have three superpowers strings don't have: items are numbered (1, 2, 3...), items can be read one at a time, and Scratch can tell you the length of the list at any moment.
Two lists, same length, same order
Today we use two lists side by side: questions and answers. The rule is simple — item N of one matches item N of the other.
questions answers
1. Capital? 1. Kuala Lumpur
2. Sabah cap? 2. Kota Kinabalu
3. Penang cap? 3. George Town
Why not one combined list?
You could store everything in one list as "Capital? || Kuala Lumpur" and split on || — but Scratch is shaky at splitting strings. Two lists is simpler to build, simpler to read, simpler to extend.
Creating the lists
Variables palette → Make a List. Name it questions. Tick For all sprites (so the Stage and every sprite can see it). Repeat for answers. You'll see two boxes (called watchers) appear on the Stage — they show the list contents live. You can drag them out of the way or untick them in the palette.
Filling the lists on green flag
If you type into the watcher by hand, the items vanish whenever you share the project. Better: build the lists in code, every time the flag is clicked. Start by emptying both lists (so a second flag click doesn't double them):
when flag clicked
delete all of [questions v]
delete all of [answers v]
add [Capital of Malaysia?] to [questions v]
add [Kuala Lumpur] to [answers v]
Reading an item back
To grab a specific question, use the reporter item ( ) of [questions v]. Drop a number (or a variable) into the round slot. item (1) of [questions v] reports the string "Capital of Malaysia?".
Worked Example — a five-question Malaysia trivia 12 min
Open Scratch. New project. Default cat (we'll use her as the quiz host). This is the very first piece of our Malaysia Trivia Quiz.
Step 1 — Make the two lists
Variables → Make a List → questions, For all sprites. Again → answers, For all sprites. Two watchers appear on the Stage.
Step 2 — Hat and wipe
On the cat, drop when ⚑ clicked. Below it: delete all of [questions v] and delete all of [answers v]. Now both lists start empty every game.
Step 3 — Add question 1
From Variables: add [ ] to [questions v]. Type Capital of Malaysia? into the slot. Below it: add [ ] to [answers v]. Type Kuala Lumpur. That's pair 1.
Step 4 — Repeat for questions 2 through 5
Drag four more add-question / add-answer pairs. Use any five Malaysia facts you like — capital of Sabah, ringgit symbol, etc. Keep them in matching order.
Step 5 — Ask question 1
Below your ten add-blocks, drop ask ( ) and wait. Into the round slot, drop item (1) of [questions v]. Click the flag, watch the cat ask "Capital of Malaysia?".
Step 6 — Check the reply
Add if < > then ... else. Into the diamond: () = (), with answer on the left and item (1) of [answers v] on the right.
Step 7 — Give feedback
Inside the then branch: say [Correct!] for (2) seconds. Inside the else: say [Nope!] for (2) seconds. Click the flag, try answering "kuala lumpur" (lower-case is fine).
The full assembled stack
when flag clicked
delete all of [questions v]
delete all of [answers v]
add [Capital of Malaysia?] to [questions v]
add [Kuala Lumpur] to [answers v]
add [Capital of Sabah?] to [questions v]
add [Kota Kinabalu] to [answers v]
add [Currency symbol of Malaysia?] to [questions v]
add [RM] to [answers v]
add [Tallest twin towers in KL?] to [questions v]
add [Petronas] to [answers v]
add [State known as Land of Hornbills?] to [questions v]
add [Sarawak] to [answers v]
ask (item (1) of [questions v]) and wait
if <(answer) = (item (1) of [answers v])> then
say [Correct!] for (2) seconds
else
say [Nope!] for (2) seconds
end
What you just built: the data layer of a real quiz game. Every Kahoot, every Quizizz, every Buzzfeed trivia uses some version of "list of questions, parallel list of answers". Yours is the same idea, just with two Scratch lists instead of a database.
Try It Yourself — grow the bank 15 min
Goal: Add a sixth Malaysia question — "National flower of Malaysia?" with the answer "Hibiscus". Then change the asking script to use item (6) of [questions v] and item (6) of [answers v].
add [National flower of Malaysia?] to [questions v]
add [Hibiscus] to [answers v]
ask (item (6) of [questions v]) and wait
if <(answer) = (item (6) of [answers v])> then
say [Correct!] for (2) seconds
else
say [Nope!] for (2) seconds
end
Think: You changed the index in two places — once on the ask, once on the check. If you only changed one, the cat would ask the new question but check the old answer. That's an alignment bug.
Goal: Loop through all the questions in order. Use length of [questions v] with a repeat ( ) and a counter variable i that goes 1, 2, 3, ...
set [i v] to (1)
repeat (length of [questions v])
ask (item (i) of [questions v]) and wait
if <(answer) = (item (i) of [answers v])> then
say [Correct!] for (1) seconds
else
say [Nope!] for (1) seconds
end
change [i v] by (1)
end
Think: length of [questions v] reports 6 right now. Add a 7th pair tomorrow and the loop automatically runs 7 times — no edit to the loop. That's why we read the length instead of hard-coding a number.
Goal: Build a second question bank on a different sprite. Make a new sprite (say, a parrot). Give it two lists: food-questions and food-answers, with 4 questions about Malaysian food (nasi lemak, rendang, teh tarik, satay). The cat asks Malaysia geography trivia; the parrot asks food trivia. Use broadcast ( ) to hand control between them.
when flag clicked
delete all of [food-questions v]
delete all of [food-answers v]
add [Rice dish wrapped in pandan leaf?] to [food-questions v]
add [Nasi Lemak] to [food-answers v]
when I receive (parrot-go v)
ask (item (1) of [food-questions v]) and wait
Think: Two sprites, two banks, one project. This is the start of a multi-round quiz game. The structure scales — five sprites with five banks each = 25 categories, no extra blocks needed beyond what you already know.
Mini-Challenge — the missing answer 5 min
"Aishah's off-by-one quiz"
Aishah is building a five-question Sabah trivia. She loads her lists like this:
when flag clicked
delete all of [questions v]
delete all of [answers v]
add [Capital of Sabah?] to [questions v]
add [Tallest mountain in Malaysia?] to [questions v]
add [Kota Kinabalu] to [answers v]
add [Mount Kinabalu] to [answers v]
ask (item (1) of [questions v]) and wait
if <(answer) = (item (1) of [answers v])> then
say [Correct!] for (2) seconds
end
Read the add-blocks slowly. What's item (1) of [questions v]? What's item (1) of [answers v]? Do they match?
Reveal one valid solution
Aishah added both questions first, then both answers. So item (1) of [questions v] is "Capital of Sabah?" but item (1) of [answers v] is "Kota Kinabalu"... wait, that actually matches! Look again.
Actually item 1 / item 1 lines up by luck. But item 2 of questions is "Tallest mountain in Malaysia?" and item 2 of answers is "Mount Kinabalu". So far so good. The real bug is more subtle: by adding all questions before any answers, Aishah set a trap for future her — when she inserts question 3 in the wrong spot, the lists will silently desync and every check from that point on will be wrong.
The fix is the discipline rule: always add in pairs. One question, then its answer. Then the next question, then its answer. The two lists grow together, item-by-item, and stay aligned no matter how many you add:
when flag clicked
delete all of [questions v]
delete all of [answers v]
add [Capital of Sabah?] to [questions v]
add [Kota Kinabalu] to [answers v]
add [Tallest mountain in Malaysia?] to [questions v]
add [Mount Kinabalu] to [answers v]
Same blocks, different order. Now adding question 3 means typing the question and the answer right next to each other — impossible to miss the pairing.
Recap 3 min
You built the data foundation of a trivia game. Two parallel lists — questions and answers — let Scratch hold dozens of prompts in numbered slots, with each prompt paired to its reply by matching index. On green flag you wipe both lists, then add pairs one at a time. To ask question N: item (N) of [questions v]. To check question N: compare answer to item (N) of [answers v]. The discipline rule — always add in matched pairs — keeps the two lists aligned forever.
- List
- A numbered, ordered Scratch data structure that holds many items in one variable. Made in the Variables palette via Make a List. Items are accessed by their position: 1, 2, 3, ...
- Parallel lists
- Two (or more) lists where item N of one matches item N of the other. Today's questions and answers are the classic example. They must grow together to stay aligned.
- Index
- The position number of an item in a list. Scratch indexes start at 1, not 0. Item 1 is the first item, item 2 is the second, and length of [questions v] tells you the last valid index.
- Watcher
- The Stage box that shows a variable or list's live contents. Useful for debugging — you can see your list fill up in real time when the flag is clicked. Untick the box in the palette to hide it from players.
- Add to list
- The add [ ] to [ v] block. Always appends to the bottom of the list, so the order you call it in is the order items end up. Pair this with delete all of [ v] at the start of every game.
Homework 2 min
Build a 10-question Malaysia geography quiz. One sprite, two lists, ten matched pairs.
- New project. Default cat or any sprite as the quiz host.
- Make two lists for all sprites:
questionsandanswers. - On when ⚑ clicked, delete all of [ v] both lists, then add 10 matched pairs about Malaysian geography — states, capitals, rivers, islands, mountains. Always add in pairs.
- After the loading, ask question 1 with ask (item (1) of [questions v]) and wait, then check the reply against item (1) of [answers v] using if < > then ... else with say [Correct!] for (2) seconds / say [Try again!] for (2) seconds.
- Stretch (optional): repeat the ask/check for question 2 and question 3, so you have a three-question demo.
Save as HW-L4-37-Malaysia-Quiz.sb3.
Bring back next class:
- The
.sb3file with all 10 pairs loaded. - Your answer to: "If you click the flag twice in a row without changing anything, how many items end up in the questions list? Why? And which one block prevents it?"
Heads up for next class: SCR-L04-38 picks questions at random instead of in order, and uses a third list to track which ones have already been asked so the quiz never repeats. Your 10-pair bank from tonight will be the input.