Skip main navigation

New offer! Get 30% off one whole year of Unlimited learning. Subscribe for just £249.99 £174.99. New subscribers only T&Cs apply

Find out more

Classifying Animals

Watch this video to see how you can make use of selection to make a program that asks questions to classify animals.
2.2
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.
18.6
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.
47.5
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.
74.9
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.
118.4
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.
135.9
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?
178.7
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.
223.5
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.
266.8
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.
303.5
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.
319.2
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.
358.9
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.
374.5
And it’s asked me the question again, so I’m going to type “Nope.”
380.5
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.
439.7
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.
479.9
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.
501.3
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.”
552.7
And so, I’m going to add a follow-up question, “Can your animal fly?”
570.6
So again, I’m using an ask block for this.
592.5
And now, if they say that the animal can fly, we know that it’s a bird.
601.7
So again, we can say “If the answer to this question is yes,” we can tell them, “Your animal is a bird.”
619.6
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.

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.

This article is from the free online

Teaching Programming in Primary Schools

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now