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 This video will demonstrate how to use selection to classify animals. I’m going to start with just two animals in my programme, a cat and a snake, which I’ll show you in a flowchart that I’ve made.

Skip to 0 minutes and 19 seconds And the way I’m going to classify them is to say that a cat is warm-blooded and a snake is cold-blooded. So in my programme I’m going to ask the question, “Is the animal you’re thinking of warm-blooded?” And if they answer, “Yes,” I’ll know they’ve chosen a cat. And if they answer otherwise, I’ll know they’ve chosen a snake. This first programme will be quite a simple one.

Skip to 0 minutes and 48 seconds So let’s start the programme when the green flag is clicked. And first of all, I’m going to use an ask block to ask the user whether their animal is warm-blooded.

Skip to 1 minute and 15 seconds I’m expecting a yes or no answer to this question. And I can then use that answer to tell the user what the animal is. So if the answer to the question, “Is your animal warm-blooded?” is, “Yes,” then I know they’ve chosen a cat. So I’m going to use an equals block, and I’m going to say, “If answer equals yes,” and then I’m going to use a say block to tell them, your animal is a cat.

Skip to 1 minute and 58 seconds And if they didn’t answer, “Yes,” to that question, I know their animal is cold-blooded. So I can tell them that that animal is a snake.

Skip to 2 minutes and 16 seconds And I need to test this programme by running it twice. First of all, I’m going to say, “Yes, my animal is warm-blooded.” And it should tell me my animal is a cat. And if I click the flag again, and this time, I’m going to answer, “No.” It knows that my animal is cold-blooded, and so it tells me my animal is a snake. Now we’ve done a pretty good job of testing our programme by running it twice. But actually, there are more than two answers to this question. If I click the flag, and I’m asked, “Is my animal warm-blooded?” what happens if I type Y-E-P, yep?

Skip to 2 minutes and 59 seconds Now I mean yep, as in yes, but my programme tells me that animal is a snake, which isn’t true. The same happens if I type, “Yes, it is,” so I’m saying that my animal is warm-blooded, but my programme is telling me that my animal is a snake. Now the reason for this is, of course, that this first block over here, that says your animal is a cat, is only run if the answer is exactly equal to yes. It has to be spelt Y-E-S. Anything else means that this block is run instead that says that your animal is a snake. And there are a few different ways to fix this. I’m going to show you a few of them.

Skip to 3 minutes and 44 seconds The first way is that we might decide the user could answer, yes, Y-E-S, or they might just answer with a Y. So I could say if the answer is yes, or if it’s just a Y. And I could use an all block to build up a longer condition and put these two answers inside it. So if I add that back into the if block, it’s now saying if the answer is equal to yes, or if the answer is just equal to a Y, then it will say, “Your animal is a cat.” So you can see now, if I answer, “Yes,” it will tell me that animal is a cat.

Skip to 4 minutes and 27 seconds And if I answer Y, it also tells me my animal is a cat. So that’s one way I can improve the programme. Another way is that we could keep asking the question, whether the animal is warm blooded, until the person gives us the answer that we want. So we could keep repeatedly asking this question until they give us a yes or no answer. And to do that, I’m going to use a repeat-until block.

Skip to 5 minutes and 4 seconds And again, I’m going to use an all block to say that I’m going to repeatedly ask until the answer is yes or the answer is no. So let’s add those into the all block.

Skip to 5 minutes and 19 seconds And then let’s add that condition into the repeat-until block, add our ask block back in. And so, first of all, it will keep asking this question until we either get yes or no as an answer. And now I know that the answer here is going to be either yes or no, then this code underneath will work as we expect it to. So let’s test this out.

Skip to 5 minutes and 59 seconds OK, let’s click the green flag, and I will type “Yep.” It’s ignored that and asked the question again, and I’m going to type a Y this time.

Skip to 6 minutes and 15 seconds And it’s asked me the question again, so I’m going to type “Nope.”

Skip to 6 minutes and 21 seconds And you’ll notice that it won’t move on until I type “Yes” or “No.” And then when I type, “Yes,” it tells me my animal is a cat. So there’s a third thing that we can do, which is that instead we could say, “Is your animal warm-blooded?” And we could say, if the answer is yes, then the animal is a cat. Otherwise we could say, if the answer is no, then your animal’s a snake. And otherwise, if it’s not yes, and also it’s not no, then we could say, “I didn’t understand.” So the way that I’m going to do that is I’m going to get another if-else block, and add this in here.

Skip to 7 minutes and 20 seconds And so, if the answer is yes, then we know the animal is a cat. If the answer is no, then we know the animal is a snake. And if the answer is neither yes or no, then we get an answer saying, “I didn’t understand.” So we need to test this three times so we can see that yes works, and that no works. And now I’ll run a third time, and I’ll type something else.

Skip to 8 minutes and 0 seconds And the sprite says “I didn’t understand.” Now I’ll take this out because I just wanted to demonstrate some ways that you can validate the user input. So we’ll put the programme back to how it was.

Skip to 8 minutes and 21 seconds OK, so let’s add a third animal to our classification programme now, which is a bird. The bird is also warm-blooded, as you’ll see in this flowchart. So it means that if we ask the question, “Is your animal warm-blooded?” and they answer yes, we need to ask a follow up question to decide whether they were thinking of a bird or a cat. So the question we’re going to ask is, “Can your animal fly?” Now we only need to ask this follow up question, “Can your animal fly?” if the animal is warm-blooded. So inside this “Is your animal warm-blooded?” I’ve answered “Yes.”

Skip to 9 minutes and 13 seconds And so, I’m going to add a follow-up question, “Can your animal fly?”

Skip to 9 minutes and 31 seconds So again, I’m using an ask block for this.

Skip to 9 minutes and 53 seconds And now, if they say that the animal can fly, we know that it’s a bird.

Skip to 10 minutes and 2 seconds So again, we can say “If the answer to this question is yes,” we can tell them, “Your animal is a bird.”

Skip to 10 minutes and 20 seconds And otherwise, if it can’t fly, we know the animal is a cat. So you’ll notice that the follow-up question, “Can your animal fly?” is only inside this bit here, where they’ve answered, “Yes, the animal is warm-blooded.” So let’s start with the easy test and say, “No, your animal is a snake.” And then, let’s try again and say, “Yes, the animal is warm-blooded.” And now, it asks if my animal can fly. If I say yes to this question, it will tell me that my animal is a bird. And if I test for a third time, and say that my animal is warm-blooded, but no, it can’t fly, then it tells me my animal is a cat.

Classifying Animals

This video shows how you can use selection to make a program that classifies animals, as you’ve just seen in the demo.

You’ll see Katharine make the program identify three animals (you will add a fourth one later). She’ll also show some ideas for checking your program understands the answers it’s given.

Share this video:

This video is from the free online course:

Teaching Programming in Primary Schools

Raspberry Pi Foundation