Learning Goals 3 min
By the end of this lesson you will be able to:
- Store quiz content in two parallel lists — questions and answers — where index
Nof one always lines up with indexNof the other. - Loop through both lists with a single repeat (length of questions), asking each question with ask () and wait and comparing the player's reply to the matching answer.
- Append the final score to a high-scores list and display the top five — sorted high-to-low — so every replay shows the leaderboard.
Warm-Up — Mei's quiz that always says wrong 7 min
Mei tries to build a one-question quiz. She wires it up like this:
when flag clicked
ask [What is 7 x 8?] and wait
if <(answer) = [56]> then
say [Correct!] for (2) seconds
else
say [Wrong!] for (2) seconds
end
56 in the answer box. The cat says "Wrong!" every single time.What's going on? She typed 56. The answer is 56. Predict why Scratch disagrees, then peek.
Reveal the answer
The script is correct. The most common reason Mei sees "Wrong" is one of two tiny things — extra whitespace or wrong block. The compare () = () matches both numbers and text and ignores nothing — "56 " (with a space) is not equal to "56". Also check the answer slot is using the round answer reporter from Sensing, not a typed-in [answer] string. In every quiz you ever build, the compare step is where bugs hide.
Today we scale this from one question to ten — using lists. The compare logic stays the same; the loop around it does the heavy lifting.
New Concept — parallel lists 15 min
You've used lists since L03-07. You know how to add, delete, and item () of. Today's trick is using two lists at once, lined up index by index.
What "parallel" means
Two lists are parallel when the same index in each one belongs together. We'll make:
questions answers
1. Capital of Malaysia? 1. Kuala Lumpur
2. 7 x 8? 2. 56
3. Colour of the sky? 3. blue
4. ... 4. ...
item (3) of [questions v] belongs with item (3) of [answers v].Whenever we read question i, we know the right answer is at the same index i of the answers list. That index — held in a variable usually called i — is what ties them together.
The loop pattern
Asking ten questions one at a time would be ten copies of ask. With parallel lists, you do it with one ask inside a repeat:
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
change [score v] by (1)
end
change [i v] by (1)
end
That's the headline pattern of this lesson. Memorise it. You'll re-use it in vocab quizzes, surveys, multi-step adventures, and any "ask a sequence of things" game.
Why a separate index variable?
You could try to use item # of (answer) in [questions v] to "look up" the question — but that searches for a match, which is the opposite of what we want here. We have the index already (the loop counter). We just need to use it on both lists.
Saving the score
When the quiz ends, score holds a number 0–10. Add it to a third list called high-scores:
add (score) to [high-scores v]
Lists in Scratch persist across project runs inside the editor — so the high scores from this morning's play are still there this afternoon. (They reset if you close and reopen the file in some setups; cloud lists, which we'll meet in L03-46+, fix that for good.)
Sorting the leaderboard
Showing the top 5 needs the scores in order, biggest first. Real bubble-sort is messy in Scratch. For our first leaderboard we'll cheat in a small but useful way:
- Find the biggest number in high-scores using a tiny loop.
- Show it. Delete it from the list.
- Repeat 4 more times.
- Add the five removed values back at the end so the list isn't ruined.
That's a destructive sort — we wreck the list to read it. We patch it back. For a class project, that's fine. (For homework, you'll meet a non-destructive version.)
Worked Example — building the trivia quiz 25 min
Open Scratch. New project. Default cat. Nine steps.
Step 1 — Make the three lists
Variables → Make a List. Create three: questions, answers, high-scores. Tick the watchers for all three so we can see them while building.
Step 2 — Fill the question and answer lists
Click each list watcher's + button and type in 10 entries. Keep the index alignment perfect — question 1 lines up with answer 1, etc. Suggested local trivia:
- What is the capital of Malaysia? → kuala lumpur
- How many states are in Malaysia? → 13
- What is 7 x 8? → 56
- Which planet is closest to the sun? → mercury
- What colour is a ripe rambutan? → red
- How many legs does a spider have? → 8
- What is the largest ocean? → pacific
- Which river runs through Kuala Lumpur? → klang
- What is 144 divided by 12? → 12
- What animal is on the Malaysian coat of arms? → tiger
Step 3 — Make the score and index variables
Variables → Make a Variable. Create score (tick the watcher) and i (untick — it's just a counter).
Step 4 — The intro
On the cat:
when flag clicked
set [score v] to (0)
set [i v] to (1)
say [Welcome to Trivia Time!] for (2) seconds
Step 5 — The ask loop
Snap this under the intro:
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
change [score v] by (1)
else
say (join [Wrong! It was ] (item (i) of [answers v])) for (2) seconds
end
change [i v] by (1)
end
Flag it. Ten questions, scored. The watcher shows your running score.
Step 6 — Save the score
After the loop:
say (join [You scored ] (join (score) [/10])) for (3) seconds
add (score) to [high-scores v]
Step 7 — Find the top score, five times
Now the leaderboard. We need two helper variables and a destructive sort. Add variables best and best-i (untick both watchers). Then this script, snapped below Step 6:
delete all of [top-five v]
repeat (5)
set [best v] to (0)
set [best-i v] to (1)
set [i v] to (1)
repeat (length of [high-scores v])
if <(item (i) of [high-scores v]) > (best)> then
set [best v] to (item (i) of [high-scores v])
set [best-i v] to (i)
end
change [i v] by (1)
end
add (best) to [top-five v]
delete (best-i) of [high-scores v]
end
You need to make one more list: top-five. Make it, tick the watcher, and reposition it on the Stage where you want the leaderboard to appear.
Step 8 — Put the high scores back
We just deleted 5 items from high-scores. Patch them back:
set [i v] to (1)
repeat (length of [top-five v])
add (item (i) of [top-five v]) to [high-scores v]
change [i v] by (1)
end
Step 9 — Bow out
say [Top 5 shown on the right! Hit the flag to play again.] for (3) seconds
The complete final stack
when flag clicked
set [score v] to (0)
set [i v] to (1)
say [Welcome to Trivia Time!] for (2) seconds
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
change [score v] by (1)
end
change [i v] by (1)
end
add (score) to [high-scores v]
What you just built: a full trivia game that scales to any number of questions, scores the player, and shows a top-5 leaderboard. Two parallel lists drove the quiz; one list plus a scan-and-delete loop drove the leaderboard. Every block came from L03-07 through L03-13.
Try It Yourself — three quiz extensions 15 min
Goal: Add a "type your name" prompt at the start, and replace add (score) to [high-scores v] with something that records both the name and the score together — like "Ahmad: 7".
ask [What's your name?] and wait
set [player v] to (answer)
add (join (player) (join [: ] (score))) to [high-scores v]
"Ahmad: 7" as one string. The high-scores list now reads like a real leaderboard.Think: The sort in Step 7 will break here — you can't compare "Ahmad: 7" with >. That's a Stretch problem for the bottom task.
Goal: Randomise the question order so each play feels different. Hint: don't shuffle the list (hard). Instead, pick the next question by random index, and delete it from a working copy of the list as you go.
delete all of [remaining v]
set [i v] to (1)
repeat (length of [questions v])
add (i) to [remaining v]
change [i v] by (1)
end
repeat (length of [remaining v])
set [pick v] to (item (pick random (1) to (length of [remaining v])) of [remaining v])
ask (item (pick) of [questions v]) and wait
if <(answer) = (item (pick) of [answers v])> then
change [score v] by (1)
end
delete (item # of (pick) in [remaining v]) of [remaining v]
end
Think: You're using three lists at once — questions, answers, and remaining. Lists scale. Variables don't.
Goal: Replace the destructive sort with a non-destructive one. Build the top-five without ever deleting from high-scores. Hint: track which indexes you've already "used" in a fourth list called used.
delete all of [top-five v]
delete all of [used v]
repeat (5)
set [best v] to (-1)
set [best-i v] to (0)
set [i v] to (1)
repeat (length of [high-scores v])
if <<(item (i) of [high-scores v]) > (best)> and <not <[used v] contains (i) ?>>> then
set [best v] to (item (i) of [high-scores v])
set [best-i v] to (i)
end
change [i v] by (1)
end
add (best) to [top-five v]
add (best-i) to [used v]
end
Think: This is closer to how real leaderboards work in real apps — the database stays intact and the leaderboard is computed on the fly. You're learning a database habit, in Scratch.
Mini-Challenge — Priya's stuck quiz 5 min
"Why does it only ask question 1?"
Priya's quiz asks the first question, scores it, then asks question 1 again. And again. Forever. Here's her loop:
when flag clicked
set [score v] to (0)
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
change [score v] by (1)
end
end
What single block is missing — and why does its absence cause "question 1 forever"?
Reveal one valid solution
There's no change [i v] by (1) at the bottom of the loop. i starts at 1 and stays at 1 every single iteration. The repeat runs 10 times (right count) but always reads item (1) of [questions v].
One block fixes it:
repeat (length of [questions v])
ask (item (i) of [questions v]) and wait
if <(answer) = (item (i) of [answers v])> then
change [score v] by (1)
end
change [i v] by (1)
end
The classic forgot-to-increment bug. Any time you use a counter variable to walk through a list, the increment block must be inside the loop. (In other languages you'd use a for loop and never have this bug — but in Scratch the increment is on you.)
Recap 3 min
You built a complete trivia quiz with a high-score leaderboard. Two parallel lists — questions and answers — store the content. A single repeat (length of [questions v]) with an index variable i walks both lists in lockstep, asking and grading each question. The final score is pushed onto a high-scores list, and a scan-find-largest-delete loop pulls out the top five into a display list. The deep idea today: data lives in lists, code stays small. Add another question — the loop handles it. Add another play — the leaderboard grows. You never have to touch the script.
- Parallel lists
- Two or more lists where the same index in each one belongs together. Used everywhere — vocabulary apps, contact lists, multi-language dictionaries. The lists must stay the same length for the pairing to work.
- Index variable
- A variable (usually called i) that holds the current position while walking through a list. Starts at 1, increments by 1, ends when the loop ends.
- Length of list
- The round reporter length of [list v] — gives the current item count. Drop it into a repeat to make a loop that auto-scales with the list size.
- Leaderboard
- A list (or display) showing the best scores across all plays, usually sorted high-to-low and trimmed to a fixed number of entries (top 5, top 10). Built today with a destructive sort.
- Destructive sort
- An algorithm that finds and removes items from the source list as it builds the sorted output. Easy to code; you must remember to put items back if the source list still matters afterwards.
- Bubble sort
- The simplest non-destructive sorting algorithm — compare every pair of neighbours and swap them if out of order, repeat until no swaps happen. Mentioned but not built today.
Homework 2 min
Make the quiz your own. Three small tasks.
- Replace the 10 trivia questions with 10 of your own — about your favourite K-pop group, footy team, anime, or family. Keep both lists exactly 10 long.
- Add a timer. Set a variable timer to timer at flag, and at the end say
"You scored 7 in (timer) seconds". - Make the top-5 leaderboard show after the score message so the player sees their own score, then sees where they ranked.
- Save as
HW-L3-44-My-Quiz.sb3.
Bring back next class:
- The
.sb3file. - Your answer to: "What would happen if you added an 11th question to the list but only 10 answers? Predict, then test."
Heads up for next class: SCR-L03-45 isn't about coding — it's about planning before you code. We'll plan a tiny game on paper using a 7-step checklist, then watch how much faster the code goes when the plan is already on the page.