A Quiz Game in Scratch
A step-by-step primary lesson on building a quiz game in Scratch: ask questions with the ask block, check the answer with if/else, keep score with a variable, and add several questions. With a worked project and quiz.
Key takeaways
- The 'ask and wait' block shows a question and stores the player's reply in 'answer'
- An 'if/else' block checks whether the answer is right and reacts to each case
- A 'score' variable counts correct answers as the quiz goes on
- You add more questions by repeating the ask, check and score pattern
What makes a quiz game?
A quiz game asks the player questions, checks if they got each one right, and keeps score. It is a brilliant first "thinking" project because it uses three of the most important ideas in coding all at once: asking for input, making a decision, and keeping score. By the end of this lesson you will have a working multi-question quiz that congratulates the player and shows their final score.
If Scratch is new to you, work through getting started with Scratch first. This project also builds directly on score-keeping, so variables and score in Scratch is a great companion lesson.
Step 1: Ask a question
The block we need lives in the blue Sensing category. It is called ask [...] and wait. When it runs, it shows a question at the bottom of the stage with a text box, then pauses until the player types a reply and presses Enter.
Click a sprite and try this:
when green flag clicked
ask [What is 2 + 2?] and wait
Click the green flag, type your reply, and press Enter. Easy! But where does the reply go? Right next to the ask block in the Sensing category is the answer block — a small rounded block that holds whatever the player just typed. We will use it in the next step.
Step 2: Check the answer with if/else
Now we decide: was the answer right or wrong? For that we use an if ... then ... else block from the orange Control category. It runs one set of blocks when something is true, and a different set when it is false. If decisions are new to you, see making decisions with if.
We compare the answer to the correct value using the = block from the green Operators category:
when green flag clicked
ask [What is 2 + 2?] and wait
if <(answer) = [4]> then
say [Correct!] for 2 seconds
else
say [Not quite!] for 2 seconds
Read it step by step:
- ask [...] and wait shows the question and stores the reply in
answer. - if (answer) = [4] asks: did the player type 4?
- If yes, the blocks under
ifrun — the sprite says "Correct!". - If no, the blocks under
elserun instead — the sprite says "Not quite!".
Click the green flag and try both a right and a wrong answer. The sprite reacts differently each time. That is decision-making in action.
Step 3: Keep score
A quiz needs a score. We make a variable to count correct answers.
- In the orange Variables category, click Make a Variable, name it
score, and click OK. - At the very start of the quiz, reset it with
set [score] to 0. Variables remember their value between runs, so without this, last game's points carry over. - When the player is right, add a point with
change [score] by 1.
Here is question one with scoring:
when green flag clicked
set [score] to 0
ask [What is 2 + 2?] and wait
if <(answer) = [4]> then
change [score] by 1
say [Correct!] for 2 seconds
else
say [Not quite!] for 2 seconds
Tick the checkbox next to score in the Variables list so a live counter shows on the stage. Now the score climbs each time the player answers correctly.
Step 4: Add more questions
A quiz with one question is a bit short! Adding more is just repeating the same pattern: ask, check, and score. Building things in order like this is called sequencing — see sequences: putting steps in order if you want a refresher.
ask [What colour is the sky on a clear day?] and wait
if <(answer) = [blue]> then
change [score] by 1
say [Correct!] for 2 seconds
else
say [It is blue!] for 2 seconds
Notice the answer comparison: Scratch ignores capital letters, so Blue and blue both match blue. That makes word questions friendlier for young players.
A worked project: a three-question quiz
Let's build a complete quiz from start to finish. It asks three questions, scores them, and shows the final result.
when green flag clicked
set [score] to 0
ask [What is 5 + 3?] and wait
if <(answer) = [8]> then
change [score] by 1
say [Correct!] for 1 seconds
else
say [The answer was 8.] for 1 seconds
ask [How many legs does a spider have?] and wait
if <(answer) = [8]> then
change [score] by 1
say [Correct!] for 1 seconds
else
say [Spiders have 8 legs!] for 1 seconds
ask [What is the first letter of the alphabet?] and wait
if <(answer) = [a]> then
change [score] by 1
say [Correct!] for 1 seconds
else
say [It is A!] for 1 seconds
say (join [You scored ] (join (score) [ out of 3!])) for 3 seconds
Here is how the whole thing runs, step by step:
- set [score] to 0 gives a clean start.
- Each question follows the same three-part pattern: ask, then if/else check, then change score if correct.
- The wrong answers give a helpful hint, so the quiz is also a little lesson.
- At the end, the join blocks build a sentence like "You scored 2 out of 3!" by gluing the words and the
scorevalue together, and the sprite says it.
Click the green flag and play your quiz from start to finish. You built a real game that asks, decides and scores!
Try it: make it your own
You have a working quiz. Now make it yours with these challenges:
- Add more questions: copy the ask-check-score pattern for a fourth and fifth question on a topic you love — animals, space, or sport.
- A perfect-score reward: at the end, use
if (score) = 3 thento play a cheering sound and a victory dance. - Accept two spellings: for tricky words, check
if <(answer) = [grey]> or <(answer) = [gray]>so both are correct. - Add a timer: use the built-in
timerand challenge the player to finish quickly.
If a correct answer is marked wrong, check the value in your = block matches exactly (remember Scratch ignores capital letters but not spaces). Fixing problems like this is called debugging, and every coder does it. Have fun making quizzes! ❓
Quick quiz
Test yourself and earn XP
What does the 'ask and wait' block do?
'ask [...] and wait' pops up a question, pauses the script, and waits for the player to type and submit a reply.
Where is the player's reply stored after they answer?
After 'ask and wait', the player's reply is kept in the built-in 'answer' block, which you can compare to the correct value.
Which block lets you do one thing if the answer is right and a different thing if it is wrong?
An 'if/else' block runs the first set of blocks when the condition is true and the 'else' blocks when it is false.
How do you add a point for a correct answer?
'change [score] by 1' adds one to the score variable, so it goes up each time the player gets a question right.
Why do we 'set [score] to 0' at the start of the quiz?
Variables remember their value between runs, so resetting to 0 at the start gives every new quiz a clean score.
FAQ
By default Scratch compares the answer exactly, but it ignores capital letters, so 'Paris' and 'paris' both match 'paris'. For number questions this is simple. For word questions, pick answers that are easy to spell, or accept a couple of spellings by checking with 'or'.
Right after an 'ask and wait' block, whatever the player typed is stored in the built-in 'answer' block, found in the Sensing category. You compare it with an '=' block to see if it is correct.
Keep exploring
More in Coding