- Learnt28 min · a loop the list ends, lives, a card
- Basic24 min · the finished quiz
- Challenge 1–327 min · medals, a best score, easy and hard
- Extrabonus · ship it
Review — where we got to 5 min
<[accepted v] contains (answer)?>searches a whole list at once.delete (1) of [questions v]shortens the list — and the rows below move up.(length of [questions v])is live: it changes as the list does.
Quick-fire
Your loop is repeat (length of [questions v]), and inside it you delete row 1 each time. How many questions get asked?
Reveal the answer
Five — the repeat reads the length once, when the loop starts, and then counts down from it. Today you need a loop that keeps checking, and that is a different block.
Today’s Topic 3 min
repeat until <(length of [questions v]) = (0)>— a loop the list ends- Two ways for a round to be over, and telling them apart
- A score out of the list, and a medal band
- A best score that the green flag must not reset
Learning outcome
By the end of this lesson you will be able to:
- Write a loop that stops when a list is empty.
- End a round two different ways and say which one happened.
- Keep a record that survives a new game.
Learnt — endings 28 min
1 · Repeat, and repeat until
repeat () looks at its number once and then counts down. If the list shrinks underneath it, the loop does not notice.
repeat until <> checks its question every time round. That is the one you want when the list is the thing that decides:
repeat until <(length of [questions v]) = (0)>
say (item (1) of [questions v]) for (1.1) seconds
delete (1) of [questions v]
end
say [The box is empty.] for (3) seconds
How to use this: Click the green flag and watch the box drain a row at a time until the loop stops itself.
2 · Lives — a second way to end
A quiz you cannot lose is not much of a quiz. Add three lives and now there are two endings, so the loop needs to watch for both:
set [lives v] to (3)
repeat until <<(length of [questions v]) = (0)> or <(lives) = (0)>>
say (item (1) of [questions v]) for (1.1) seconds
ask [Your answer?] and wait
if <[accepted v] contains (answer)?> then
change [score v] by (1)
else
change [lives v] by (-1)
end
delete (1) of [questions v]
end
<<> or <>> from Level 2, doing real work: either ending stops the loop.How to use this: Click the green flag and watch three wrong answers end the round with questions still in the box.
3 · Which ending was it?
After the loop, the two endings look different — and the card should say so. The test is just the same question the loop was asking:
if <(lives) = (0)> then
say [Out of lives!] for (2) seconds
else
say [You answered every question.] for (2) seconds
end
say (join [Score ] (join (score) (join [ out of ] (length of [answers v])))) for (3) seconds
answers, not questions — the questions list has been emptied by the loop.Basic — the finished quiz 24 min
Open your quiz. Everything today goes around what you already have.
Step 1 — save the total first
- Make a variable called
total. - Just after the questions are stocked, set it to
(length of [questions v]).
Step 2 — three lives
- Make a variable called
livesand set it to 3 under the flag. - In the wrong half of your marking if, add
change [lives v] by (-1). - Show both boxes on the Stage.
Step 3 — the loop that ends
- Change your loop to
repeat until <<(length of [questions v]) = (0)> or <(lives) = (0)>>. - Make sure the delete runs whichever way the answer went.
Step 4 — the card
- After the loop, say which ending it was.
- Then say the score out of
total.
Step 5 — test both endings
- Play once getting everything right. Check the card.
- Play once getting three wrong on purpose. Check the other card.
set [total v] to (length of [questions v])
set [score v] to (0)
set [lives v] to (3)
repeat until <<(length of [questions v]) = (0)> or <(lives) = (0)>>
say (item (1) of [questions v]) for (1.1) seconds
ask [Your answer?] and wait
if <(answer) = (item (1) of [answers v])> then
change [score v] by (1)
say [Correct!] for (0.8) seconds
else
change [lives v] by (-1)
say (join [No - it was ] (item (1) of [answers v])) for (1.2) seconds
end
delete (1) of [questions v]
delete (1) of [answers v]
end
say (join [Score ] (join (score) (join [ out of ] (total)))) for (3) seconds
total was saved before they did.How to use this: Click the green flag and watch a full round: three right, two wrong, and the card at the end.
The score is out of 0 at the end.
total is being read after the loop emptied the list. Set it before the round starts.
The quiz never ends.
A path through the loop does not delete. Check the delete is below the if / else, not inside one half of it.
It ends after one question.
Lives is being set inside the loop instead of above it, so it is 3 again every time — or it is going down by 1 on right answers too.
The answers stop matching after the first question.
Only one of the two lists is being deleted from. Both must shrink together.
Medals
Turn the score into something worth having.
- Everything right is GOLD.
- More than half is SILVER.
- Anything less says TRY AGAIN.
- The bands are worked out from the list’s length, not typed in.
How to use this: Click the green flag and watch three scores put through the same bands.
Teacher note
The gold test is <(score) = (total)>, not <(score) = (5)>. Pupils who type the 5 have written a quiz that can never be five questions long again, and the sixth question is the cheapest way to show them.
A best score that lasts
Keep the best score anybody has managed, through as many games as they like.
- A new game does not reset the best.
- A better score replaces it; a worse one does not.
- The player is told when they beat it.
How to use this: Click the green flag, then click the stage once and press G a few times. Watch the best box.
Teacher reveal
if <(score) > (best)> then
set [best v] to (score)
say [A new best!] for (1.2) seconds
end
And no set [best v] to (0) under the green flag — the exact rule from the Line Follower’s lap records in Lesson 2-20. Worth saying out loud that they have met this before; a record is a record whatever the game.
Easy and hard
Two quizzes behind one menu.
- Two lists,
easyandhard, with different questions in them. - E or H copies one of them into the list the game plays from.
- The original lists are not damaged by playing.
How to use this: Click the green flag, then click the stage once. Press E, then press H, and watch the playing list change.
Teacher reveal — why copy at all?
when [h v] key pressed
delete all of [playing v]
set [n v] to (0)
repeat (length of [hard v])
change [n v] by (1)
add (item (n) of [hard v]) to [playing v]
end
Because the game eats its list. Play straight out of hard and after one round the hard quiz is gone for good. Copying is the whole idea, and it is the same walking loop from Lesson 3-5 used to write instead of to read.
Ship it
Finish the quiz properly and share it. Everything the arc built, in one project somebody else can play.
- A title screen that says how many questions there are.
- The round, marked against accepted answers, with lives.
- A card: score, medal, and the questions you missed.
- A play-again key that does not need the green flag.
How to use this: Click the green flag and watch a whole game: title, five questions, the card and the revision list.
Teacher reveal — the shape, not the blocks
when flag clicked
delete all of [missed v]
say [QUIZ MASTER] for (1.4) seconds
say (join (length of [questions v]) [ questions. Here we go.]) for (1.6) seconds
Every block in this mission has appeared in Lessons 3-5 to 3-8. If a pupil is stuck, the answer is almost always “you built that two weeks ago” — send them back to their own project rather than to a new block.
Summary 5 min
repeat ()reads its number once.repeat until <>checks every time round.- A list can end a loop: stop when there is nothing left in the box.
- Two endings need
<<> or <>>— and a card that says which one happened. - Save the total before the loop empties the list, or the score is out of 0.
- A record is never set under the green flag.
- Repeat until
- A loop that checks its question before every go.
- Game state
- Everything the game needs to know right now — here, the list and the lives.
- Score card
- The screen at the end that says how it went.
- Record
- A best that survives a new game.
Hand in
Share your finished quiz and send the link to your teacher. It should take questions from a list, mark them fairly, end two different ways and remember your best.
Next lesson starts the last arc of this run — a card game, and the first two real algorithms: searching a list, and putting one in order.