- Learnt28 min · letters, a split My Block, a bank of lines
- Basic25 min · a five-question quiz
- Challenge 1–327 min · only 1, 2 or 3, tell the answer, four answers
- Extrabonus · a bank checker
Review — two quizzes you have built 5 min
- Level 1’s Quiz Show asked each question with its own
askblock. A new question meant new blocks. - Level 3’s Quiz Master kept questions in one list and answers in another, in step. A new question meant typing into two lists, in the same row.
- Kart Racer IV ended the last arc: a rival that drove waypoints from lists, start lights and a finish.
Quick-fire
In Quiz Master, somebody adds a question to the questions list but forgets the answers list. What goes wrong?
Reveal the answer
Every answer after that point is off by one — question 6 is checked against answer 7. Two lists in step can slip. Today a question and its answers live on one line, so they cannot.
Today’s Topic 3 min
- Text is made of letters:
(letter (i) of (line))and(length of (line)) - A
splitMy Block that cuts a line at every|into apartslist - A bank of lines that plays as a quiz
Learning outcome
By the end of this lesson you will be able to:
- Walk through text one letter at a time.
- Turn one line of text into a list of parts.
- Build a quiz where a new question is one new line, not new blocks.
Learnt — cutting a line apart 28 min
1 · Text is a row of letters
Scratch can read text one letter at a time. (length of (line)) says how many letters there are. (letter (4) of (line)) gives you the fourth.
when flag clicked
set [line v] to [bee|hive]
say (join [length of (line) = ] (length of (line))) for (1.2) seconds
set [i v] to (1)
repeat (length of (line))
say (join [letter ] (join (i) (join [ is ] (letter (i) of (line))))) for (0.5) seconds
change [i v] by (1)
end
length of here is the green Operators one, with a round slot — not the orange one for lists.How to use this: Click the green flag to start it. Nothing else to press — Kiran reads the line out one letter at a time.
2 · The split My Block
Walk the line. Every letter that is not a | joins onto the end of word. A | means the word is finished: add it to parts and start a new one.
define split (text)
delete all of [parts v]
set [word v] to []
set [i v] to (1)
repeat (length of (text))
set [letter v] to (letter (i) of (text))
if <(letter) = [|]> then
add (word) to [parts v]
set [word v] to []
else
set [word v] to (join (word) (letter))
end
change [i v] by (1)
end
add (word) to [parts v]
text, and tick Run without screen refresh so a line is cut in one go.How to use this: Click the green flag to start it. Nothing else to press — watch the parts list fill.
How to use this: Click the green flag to start it. Nothing else to press.
How to use this: Click the green flag to start it. Nothing else to press — two lines are split, one after the other.
3 · Playing one line
After the split, the question is part 1 and the answers are parts 2, 3 and 4. The player types a number. Answer 1 is part 2, so the answer they chose is part (their number + 1).
split [How many legs does a spider have?|6|10|8|8] :: custom
ask (join (item (1) of [parts v]) (join [ 1) ] (join (item (2) of [parts v]) (join [ 2) ] (join (item (3) of [parts v]) (join [ 3) ] (item (4) of [parts v]))))))) and wait
set [pick v] to (answer)
if <(item ((pick) + (1)) of [parts v]) = (item (5) of [parts v])> then
change [score v] by (1)
say [Right!] for (0.8) seconds
else
say [No!] for (0.8) seconds
end
How to use this: Click the green flag, then type 1, 2 or 3 and press Enter.
4 · A bank of lines
Put five lines in a list called bank. One loop splits line 1 and asks it, then line 2, and so on.
when flag clicked
set [score v] to (0)
set [q v] to (1)
repeat (length of [bank v])
split (item (q) of [bank v]) :: custom
ask (join (item (1) of [parts v]) (join [ 1) ] (join (item (2) of [parts v]) (join [ 2) ] (join (item (3) of [parts v]) (join [ 3) ] (item (4) of [parts v]))))))) and wait
set [pick v] to (answer)
if <(item ((pick) + (1)) of [parts v]) = (item (5) of [parts v])> then
change [score v] by (1)
say [Right!] for (0.8) seconds
else
say [No!] for (0.8) seconds
end
change [q v] by (1)
end
How to use this: Click the green flag, then answer each question: type 1, 2 or 3 and press Enter.
Now add a sixth line to bank — a question about the largest animal. Change nothing else.
How to use this: Click the green flag, then type 1, 2 or 3 and press Enter for each question.
Basic — a five-question quiz 25 min
Start a new project. Choose the Kiran sprite and our studio backdrop.
Step 1 — the stage and the lists
- Save our backdrop, quiz-studio.svg, and use Upload Backdrop.
- Make two lists, For all sprites:
bankandparts. Make the variablesscore,q,pick,word,iandletter. - Type five lines into
bank, each likeWhat do bees make?|honey|milk|silk|honey. Write your own questions, or use ours.
Step 2 — split
- Make a Block called
split, with a text inputtext. Tick Run without screen refresh. - Build its blocks from Learnt section 2.
Step 3 — the quiz
- Build the loop from Learnt section 4 on Kiran.
- Before it, add a welcome:
say [Welcome to the quiz! Type 1, 2 or 3.] for (1.5) seconds. - After it, say the score:
say (join [You scored ] (join (score) (join [ out of ] (length of [bank v])))).
How to use this: Click the green flag, then answer each question: type 1, 2 or 3 and press Enter.
Every answer is marked wrong.
Check a line in bank. A space after a | becomes part of the word, so 8 and 8 do not match. Type the lines with no spaces round the bars.
The question shows, but the answers are blank.
parts only holds one thing — the whole line. The if inside split compares with the wrong character. It must be the bar, |. Copy it from one of your bank lines if you cannot find it on your keyboard.
Question 2 asks question 1 again.
change [q v] by (1) is missing from the loop, or it is outside it.
Only 1, 2 or 3
Close the hole where typing 4 always wins.
- After the ask, check the number is 1, 2 or 3.
- If it is not, ask “Type 1, 2 or 3.” — and keep asking until it is.
- Count how many times you had to ask again.
How to use this: Click the green flag, then answer each question. Try typing 4 or 9 to see it asked again.
Teacher reveal
Straight after set [pick v] to (answer):
repeat until <<(pick) > (0)> and <(pick) < (4)>>
change [asked again v] by (1)
ask [Type 1, 2 or 3.] and wait
set [pick v] to (answer)
end
A word like cat is not more than 0, so it is asked again too.
Tell them the answer
A quiz should teach, not just mark.
- A wrong answer says “No - it was …” with the right answer.
- The right answer comes from the line, not from a block you typed.
How to use this: Click the green flag, then type 1, 2 or 3 and press Enter. Get one wrong on purpose.
Teacher reveal
In the else of the check:
say (join [No - it was ] (item (5) of [parts v])) for (1.2) seconds
Four answers
Give every question a fourth answer — without writing “4” anywhere.
- Lines now have six parts: the question, four answers, the right one.
- Use
(length of [parts v])to find the right answer: it is always the last part. - Build the question text with a loop, so a line could have any number of answers.
How to use this: Click the green flag, then type 1, 2, 3 or 4 and press Enter.
Teacher reveal
split (item (q) of [bank v]) :: custom
set [last v] to (length of [parts v])
set [question v] to (item (1) of [parts v])
set [k v] to (1)
repeat ((last) - (2))
set [question v] to (join (question) (join [ ] (join (k) (join [) ] (item ((k) + (1)) of [parts v])))))
change [k v] by (1)
end
ask (question) and wait
set [pick v] to (answer)
if <(item ((pick) + (1)) of [parts v]) = (item (last) of [parts v])> then
change [score v] by (1)
end
Six parts, take away the question and the answer, leaves four. Add Challenge 1’s guard with (last) − (1) in place of 4.
A bank checker
A line with a missing bar breaks a question halfway through a game. Find it before anyone plays.
- Split every line in the bank, one after another.
- Any line that does not make exactly five parts is reported, by its number.
- If they are all fine, say so.
How to use this: Click the green flag to start it. Nothing else to press — the bank has one broken line in it.
Teacher reveal
set [bad v] to (0)
set [q v] to (1)
repeat (length of [bank v])
split (item (q) of [bank v]) :: custom
if <<not <(length of [parts v]) = (5)>> and <(bad) = (0)>> then
set [bad v] to (q)
set [bad parts v] to (length of [parts v])
end
change [q v] by (1)
end
Summary 5 min
- Text is a row of letters:
length ofcounts them,letter (i) ofreads one. splitcuts a line at every|into a list of parts. The last part is added after the loop.- A quiz made of lines grows by one line, not by new blocks.
- Check what a player types before you use it.
- Separator
- A character that marks where one part ends and the next begins — here, the bar |.
- Split
- Cutting text into parts at every separator.
- Bank
- The list of questions a quiz is played from.
Hand in
Save your quiz. Next lesson it stops asking questions in the same order every time — and the right answer stops sitting in the same place.