- Learnt28 min · a list, add, item (N) of, length of
- Basic24 min · a quiz that walks its own list
- Challenge 1–327 min · a sixth question, a progress line, the last one
- Extrabonus · a score out of the list
Review — the Vending Machine arc 5 min
([floor v] of ())and(() mod ())break an amount into coins.(pick random (1) to (6))makes a machine different every run.- A variable is a box with a name, and it holds exactly one thing.
Quick-fire
Your machine sold three snacks. Where did it keep the three prices while it added them up?
Reveal the answer
It did not keep them. It added each one into a basket and threw it away. Today you get a box that can keep all three — and read them back.
Today’s Topic 3 min
- A list — a variable that holds many things, in order
add [thing] to [questions v](item (1) of [questions v])— and why rows start at 1(length of [questions v])— how many are in there
Learning outcome
By the end of this lesson you will be able to:
- Make a list and put things into it.
- Read any row back out by its number.
- Write one loop that works on a list of any length.
Learnt — the box that holds a quiz 28 min
1 · Why a variable is not enough
You want a quiz with five questions. With variables that is q1, q2, q3, q4, q5 — five boxes, five names, and five copies of nearly the same blocks.
Then somebody asks for a sixth question, and you have to edit the code.
A list is one box with many rows in it. Five questions, one name, and a sixth costs you nothing.
2 · Making one, and filling it
- Go to Variables and click Make a List.
- Call it
questions, for all sprites. - A tall empty box appears on the Stage. That is the list.
- Drag out
add [thing] to [questions v]and type a question into it. - Click the block. Watch a row appear in the box.
when flag clicked
delete all of [questions v]
add [What colour is a banana?] to [questions v]
add [How many legs has a spider?] to [questions v]
add [What do bees make?] to [questions v]
add [Which planet do we live on?] to [questions v]
add [What is frozen water called?] to [questions v]
delete all at the top matters — see the Watch out below.How to use this: Click the green flag and watch the box on the left fill up, one row at a time.
3 · Reading one row back
Every row has a number. In Scratch, the first row is number 1 — not 0.
(item (1) of [questions v])
(item (3) of [questions v])
4 · How many are in there?
(length of [questions v])
Put the two together and you can reach the last row of a list without knowing how long it is:
say (item (length of [questions v]) of [questions v])
How to use this: Click the green flag and watch the host read item 1, item 3, the count, and the last row.
5 · Walking the whole list
A counter and a repeat is all it takes. The counter goes 1, 2, 3… and (item (n) of [questions v]) follows it down the rows:
when flag clicked
set [n v] to (0)
repeat (length of [questions v])
change [n v] by (1)
say (item (n) of [questions v]) for (1.6) seconds
end
Basic — a quiz that walks its own list 24 min
Start a new project. Pick a host sprite and a backdrop you like.
Step 1 — the list
- Make a list called
questions. - Under the green flag, put
delete all of [questions v]. - Add five questions of your own, one block each.
- Press the flag twice and check you still have five rows, not ten.
Step 2 — read one out
- Add
say (item (1) of [questions v]) for (2) seconds. - Change the 1 to a 4 and run it again.
Step 3 — the counter
- Make a variable called
nand set it to 0 under the flag. - Add
repeat (length of [questions v]). - Inside it:
change [n v] by (1), then a say using(item (n) of [questions v]).
Step 4 — number them
- Say
(join [Q] (n))before each question. - Check the numbers on the Stage match the rows in the box.
Step 5 — prove it scales
- Add a sixth question with one more add block.
- Run it. Do not touch the loop.
- Now delete two questions from the top of the script and run it again.
when flag clicked
delete all of [questions v]
add [What colour is a banana?] to [questions v]
add [How many legs has a spider?] to [questions v]
add [What do bees make?] to [questions v]
set [n v] to (0)
repeat (length of [questions v])
change [n v] by (1)
say (join [Q] (n)) for (0.5) seconds
say (item (n) of [questions v]) for (1.6) seconds
end
say [That is the lot.] for (2) seconds
How to use this: Click the green flag and watch it walk the list from row 1 to the end.
The list grows every time I press the green flag.
delete all of [questions v] is missing at the top. A list is the one thing the flag does not reset for you.
The first bubble is empty, then it asks four questions.
change [n v] by (1) is below the say instead of above it, so the first time round it asked for item 0.
It asks the same question five times.
The say has (item (1) of [questions v]) in it rather than (item (n) of [questions v]).
It stops after three questions even though there are five.
The repeat has a typed number in it. Drag (length of [questions v]) into that slot.
A sixth question, free
Prove your quiz grows on its own.
- Add a sixth question with one add block.
- Change nothing else at all.
- The quiz asks six, and says so at the end.
How to use this: Click the green flag. Six questions go by, and the loop never knew there were six.
Teacher note
Worth doing out loud: ask the class to predict what happens, then add the question. The pupils who expected to edit the repeat are the ones who have not yet believed that (length of [questions v]) is live.
How many to go
Tell the player where they are up to.
- Before each question: “Q3 of 5”.
- After it: how many are still to come.
- Both numbers come from the list, not from anything typed.
How to use this: Click the green flag and read the two boxes as the counter climbs.
Teacher reveal
say (join [Q] (join (n) (join [ of ] (length of [questions v])))) for (1) seconds
set [to go v] to ((length of [questions v]) - (n))
The subtract is the interesting block, not the joins. Ask what to go should be on the last question, and whether their code agrees.
Name the last one
Make a fuss of the final question.
- Before the last question only, the host says “Last one!”.
- It works whether the list has three questions or thirty.
- At the end, the host repeats what the last question was.
How to use this: Click the green flag and watch for the “Last one!” just before the fifth question.
Teacher reveal
if <(n) = (length of [questions v])> then
say [Last one!] for (1) seconds
end
say (item (n) of [questions v]) for (1.6) seconds
And at the end, (item (length of [questions v]) of [questions v]) — a length used as a row number, which is the first time the two blocks have been nested into each other. Pupils who hard-code a 5 in the if have solved today’s list and no other.
A score out of the list
Add a second list of answers, mark each question against it, and give a score at the end — out of however many questions there are.
- A second list,
answers, with the answers in the same order. - Row 3 of the answers is the answer to row 3 of the questions.
- The score card reads “2 out of 5”, worked out from the list.
How to use this: Click the green flag and watch all five marked, then the card at the end.
Teacher reveal
say (item (n) of [questions v]) for (1) seconds
if <(answer) = (item (n) of [answers v])> then
change [score v] by (1)
say [Right!] for (1) seconds
else
say (join [No - it is ] (item (n) of [answers v])) for (1) seconds
end
The demo above marks on the question number rather than on typing, so the class can watch both branches without anybody having to be wrong on purpose. The real thing uses (answer), as printed here.
Summary 5 min
- A list is one box that holds many things, in rows, in order.
add [thing] to [questions v]puts a row on the end.(item (3) of [questions v])reads one row back. Rows start at 1.(length of [questions v])counts them — so a loop can handle a list of any size.- A list survives the green flag. Start with
delete all of [questions v]or it grows every run.
- List
- A variable that holds many values, numbered from 1.
- Item
- One row of a list.
- Index
- A row’s number.
- Length
- How many rows a list has right now.
Hand in
Save your quiz. Next lesson the list stops being something you fill in at the start: questions get deleted as they are answered, and new ones dropped into the middle.