- Learnt28 min · contains, as a yes and as a no
- Basic24 min · a quiz that marks itself fairly
- Challenge 1–327 min · banned words, multiple choice, two lists
- Extrabonus · what = cannot do
Review — where we got to 5 min
delete (1) of [questions v]— and everything below moves up.insert [q] at (2) of [questions v]pushes the rest down.replace item (2) of [questions v] with [q]swaps in place.
Quick-fire
Your quiz asks how many legs a spider has. A pupil types eight and it marks them wrong. Why?
Reveal the answer
The code compares the answer with 8 and nothing else. There is more than one way to be right, and today is about letting your quiz know that.
Today’s Topic 3 min
<[accepted v] contains [8]?>— searching a whole list at once- Using it as a yes: is this answer one of the right ones?
- Using it as a no: is this answer one we do not allow?
- Two lists kept in step, so row N of each belongs together
Learning outcome
By the end of this lesson you will be able to:
- Check a value against every row of a list with one block.
- Accept several different right answers to one question.
- Say what contains can do that a plain equals cannot.
Learnt — searching without a loop 28 min
1 · The register
Start with the simplest version. A quiz night has a list of players, and only people on it may join:
<[players v] contains (answer)?>
- Make a list called
playersand put four names in it. - Add
ask [What is your name?] and wait. - Drag
<[players v] contains ()?>into anif / else. - Drop
(answer)into its slot. - Welcome them in one half, turn them away in the other.
How to use this: Click the green flag, then click the stage once. Type Mia to be let in, or any other name to be turned away.
2 · More than one way to be right
Now the real use. “How many legs has a spider?” has one answer and several spellings of it. Put every acceptable one in a list:
when flag clicked
delete all of [accepted v]
add [8] to [accepted v]
add [eight] to [accepted v]
add [viii] to [accepted v]
ask [How many legs has a spider?] and wait
if <[accepted v] contains (answer)?> then
say [Correct!] for (2) seconds
else
say [Not quite.] for (2) seconds
end
How to use this: Click the green flag and watch four answers tried in turn against the same list.
3 · The same block, as a “no”
Turn it round with <not <>> and the same list keeps things out. A list of answers that are not really answers:
if <not <[banned v] contains (answer)?>> then
say [Now that is an answer.] for (2) seconds
else
say [Have a proper go.] for (2) seconds
end
4 · Two lists, one row number
Your quiz already keeps questions in one list. Keep the answers in a second one, in the same order, and row 3 of each belongs together:
(item (3) of [questions v])
(item (3) of [answers v])
That is how one loop can ask and mark every question — the same n reaches into both boxes.
Basic — a quiz that marks itself fairly 24 min
Open your quiz. You are replacing one equals with one contains.
Step 1 — the accepted list
- Make a list called
accepted. - Empty it under the flag, then add every spelling of your first question’s answer.
- Include the capitalised one and any obvious synonym.
Step 2 — mark with it
- Find the if that marks the answer.
- Drag the
<() = ()>out of its hexagon and throw it away. - Drop
<[accepted v] contains (answer)?>in instead.
Step 3 — be helpful when they are wrong
- In the wrong half, say what they typed back with a
join. - Then show one right answer with
(item (1) of [accepted v]).
Step 4 — test it properly
- Type the answer in lower case. It should pass.
- Type it in capitals. It should pass.
- Type a synonym you added. It should pass.
- Type something wrong. It should fail, and tell you what was right.
when flag clicked
set [score v] to (0)
delete all of [accepted v]
add [yellow] to [accepted v]
add [gold] to [accepted v]
ask [What colour is a banana?] and wait
if <[accepted v] contains (answer)?> then
change [score v] by (1)
say [Correct!] for (2) seconds
else
say (join [No. ] (join (answer) [ is not one of these:])) for (2.5) seconds
say (item (1) of [accepted v]) for (1.5) seconds
end
say (join [Score: ] (score)) for (3) seconds
How to use this: Click the green flag, then click the stage once. Type yellow to be marked right, or anything else to see the wrong branch.
Everything is marked wrong, even the right answer.
The list is empty when the check runs. A delete all of [accepted v] below the adds wipes them out again — it belongs above.
Everything is marked right.
The hexagon is empty, and an empty hexagon counts as false — so check you have not put the contains in the else half by mistake.
“yellow ” with a space is marked wrong.
Correct — and annoying. A row in the list has to match the whole answer. See the Extra mission for what to do about it.
The accepted box shows the answers to the player.
Untick it on the Stage once you have finished testing, or your quiz gives itself away.
No giving up
Some answers are not answers. Keep them out with a list.
- A list called
bannedwith at least four non-answers in it. - Typing one of them does not count as a go.
- Anything else is accepted as a real attempt.
How to use this: Click the green flag and watch three attempts checked against the banned list.
Teacher note
The interesting bit is the <not <>>. Pupils often build it the other way round and end up only accepting the banned words — which is worth letting them run once before you say anything.
Multiple choice
Show four options — and read them off a list rather than typing them into the code.
- A list called
choicesholding A, B, C and D. - The host reads all four out with one loop.
- The right one is named by its row number, not by its text.
How to use this: Click the green flag and watch the four options read out, then the answer given.
Teacher reveal
set [n v] to (0)
repeat (length of [choices v])
change [n v] by (1)
say (item (n) of [choices v]) for (1.1) seconds
end
say (join [The answer is ] (item (2) of [choices v])) for (3) seconds
Same walking loop as Lesson 3-5, on a different list. The point to draw out is that the options and the code are now separate things — a pupil can write a whole new question without opening the scripts.
Two lists in step
Mark every question in your quiz with one loop and two lists.
questionsandanswers, the same length, in the same order.- One counter reaches into both.
- The host can read out any question with its answer.
How to use this: Click the green flag. Row 3 of each list is shown first, then all five pairs go by.
Teacher reveal — the pairing is a promise, not a rule
repeat (length of [questions v])
change [n v] by (1)
say (join (item (n) of [questions v]) (join [ = ] (item (n) of [answers v]))) for (1.3) seconds
end
Nothing in Scratch makes the two lists match up — the pupil keeps that promise by hand, and the moment they add to one and not the other it breaks silently. Ask them to break it on purpose and watch. The tidy answer is Lesson 3-14’s My Block: one place that adds to both.
What equals cannot do
Find out for yourself exactly what a list buys you. Four people type an answer; count how many a plain = marks right, and how many contains marks right.
- Four attempts: the answer, the answer in capitals, the answer with a space after it, and a synonym.
- One counter for
<() = ()>, one for contains. - The two numbers are different, and you can say exactly why.
How to use this: Click the green flag and watch the two counters at the end. They do not match.
Teacher reveal
if <(tried) = [honey]> then
change [strict v] by (1)
end
if <[accepted v] contains (tried)?> then
change [kind v] by (1)
end
Run it in front of the class and ask them to predict the two numbers first. Nearly everybody predicts 1 for the equals, because they expect capitals to break it. The answer is 2, and being wrong about that in public is the fastest way to remember what Scratch’s equals actually does.
Summary 5 min
<[accepted v] contains (answer)?>searches every row and reports true or false.- One block does what a counter, a loop and an if would otherwise have to.
- Wrapped in
<not <>>, the same block keeps things out instead of letting them in. - Two lists in step share a row number — and it is you, not Scratch, who keeps them in step.
- Scratch’s
<() = ()>already ignores capitals. It is spaces and synonyms that need a list.
- Contains
- True when a value is one of a list’s rows.
- Search
- Looking through a list for something.
- Accepted answers
- A list of every reply that should count as right.
- In step
- Two lists whose row numbers line up.
Hand in
Save your quiz. Next lesson is the last of the arc: a round that knows when it is over, three lives, a score card, and a best score that survives the green flag.