Want to keep learning?

This content is taken from the Raspberry Pi Foundation & National Centre for Computing Education's online course, Teaching Programming in Primary Schools. Join the course to learn more.

Skip to 0 minutes and 2 seconds In this video, I’m going to show you how to create a quiz containing two questions. We’ll then improve the quiz by adding more questions or even changing the theme of the quiz. So the first thing I want to do when my project is started is I want to ask the user the first question. And so I’m going to use an ask block to do that.

Skip to 0 minutes and 27 seconds And the theme of my quiz is capital cities. So as a first question, I’m going to ask what is the capital of Germany.

Skip to 0 minutes and 47 seconds And if I click the flag to test this code, you’ll see that the sprite asks the question and waits for me to type in the answer. So if I do type in the answer– there we are– that is then stored in a variable called answer here. And I can view this on the stage by clicking the tick the box next to the variable name. Now that the user has entered the answer, I need to check whether what they’ve entered is correct. And the way that I’m going to do that is click on the Operator section. And I’m going to use this equals block.

Skip to 1 minute and 24 seconds Now, what this block does is it compares two things to see whether they’re equal and gives us a true or false answer. So I’m going to check whether my answer block or the value stored in my answer variable is equal to the word Berlin.

Skip to 1 minute and 48 seconds And then I can test this by clicking on the block, and it should return the value true.

Skip to 1 minute and 55 seconds And I don’t see true. I see false. And that’s because I put a space after the word Berlin. So I’m actually seeing whether the answer that’s been entered is equal to the text Berlin with a space after it. And so the left-hand side and the right-hand side are not identical. So I can fix this by removing the space after the word Berlin. And now I can check again, and you’ll see that the value that’s returned is true. So I’m going to use this check, whether the answer is equal to Berlin, to decide what to do next. And I’m going to use an if block. I’m going to put my answer equals Berlin true or false check inside the if block.

Skip to 2 minutes and 47 seconds And what I can say now is that if the answer is equal to Berlin, I would like the sprite to say well done. So that’s in the look section.

Skip to 3 minutes and 2 seconds And I’ll click the flag to test the code again.

Skip to 3 minutes and 7 seconds And I’ve asked the capital of Germany, so I’ll type Berlin and press Enter. And this time, my sprite will say well done. To fully test this code, though, I should really test it a second time and enter something else. So this time, I’ll enter an incorrect answer, Paris. And you’ll see that nothing happens because the code is saying that if the answer is equal to Berlin, do something, and if the answer isn’t equal to Berlin, it will just miss that block out. So there’s no more code to do anything at the moment at all. And I would like my sprite to say incorrect if the answer is incorrect.

Skip to 3 minutes and 58 seconds So I’m going to replace this if block with an if else block, and I’m going to perform the same check at the start. And then if the answer is equal to Berlin, the sprite will say well done. And if the answer isn’t equal to Berlin, the sprite will say something else now. And I’ll just get the sprite to say no. And again, to fully test this code, I should test it twice, once by entering the word Berlin, so to check that it says well done. And then test a second time and type a wrong answer, and my sprite says no. Now, of course you can add more than one code block into an if else block.

Skip to 4 minutes and 49 seconds So something else I’m going to do is to play a sound, but only if the correct answer is given. So sounds are in the sound section. I’m going to check by clicking on the Sounds tab to see what other sounds there are available. I’ve actually preloaded a cheer sound into this section which I’m going to use. If I wanted to use a different sound, I could click [INAUDIBLE] on the tab and choose one of these. [BEEPING] There are lots of different options to choose from. [APPLAUSE]

Skip to 5 minutes and 30 seconds And because I want the sound only to be played if the correct answer is given, I’m going to go back into the code section, and I’m going to use a play sound block. And I’m going to put that into the first part of my if block so it will only happen if the correct answer is given.

Skip to 5 minutes and 53 seconds So let’s test this out.

Skip to 6 minutes and 4 seconds So now we’ve got our sound. I’m going to add a second question. And the way that I’m going to do that is to right click and duplicate this code. And I’m going to not add it onto the main script just yet so that I can keep it separate for testing. For the second question, I’m going to ask what is the capital of Denmark. So I’m going to change this text in the ask block here.

Skip to 6 minutes and 36 seconds And then I just need to make another change. So I need to change the correct answer for this question. So I need to say if the answer is equal to the word Copenhagen. So this is very similar to the code for the first section. I’m just changing the question and the answer. And then I can click the block to test it.

Skip to 7 minutes and 9 seconds Type in the correct answer.

Skip to 7 minutes and 14 seconds [APPLAUSE]

Skip to 7 minutes and 21 seconds Fantastic. And then I can test it again by entering an incorrect answer to make sure that my sprite also says no.

Skip to 7 minutes and 35 seconds So now I can add the second question to the first one so that I can try the entire quiz. So I’m just going to put that in there. And I’m going to test that out now by clicking on the green flag. So I’m going to type the correct answer.

Skip to 7 minutes and 59 seconds [APPLAUSE] And now the quiz has stopped. And you can see it hasn’t asked me the second question. And the reason for this is that the code to ask the question, well, I’ve accidentally put it inside the else portion of the if else block. So the code asks the first question. If the answer is Berlin, it plays the sound, and it says well done. But then it misses out this code, and there’s nothing else for it to do. So it’s only going to ask me the second question if I get the first question wrong, and that’s not what we want. But I’ll show you how it works.

Skip to 8 minutes and 39 seconds So if I click on the green flag and type a wrong answer in, you’ll see at the moment the code says no and then asks me the second question. So what I need to do is I need to correct this by dragging this section of code and making sure it’s outside the else portion of the first question and answer. And this is quite a common mistake. And it shows that even though I had initially tested this second block, it’s really worth testing your entire script as well once you’ve added new code to it.

Making a Quiz

In this video, you’ll learn how to use selection to make a quiz.

Share this video:

This video is from the free online course:

Teaching Programming in Primary Schools

Raspberry Pi Foundation