Learning Intention

  • I can use a conditional to make a robot respond to an answer

Introduction

Conditionals are instructions in a program that perform different actions depending on whether their condition is true or false. They take in information and then, depending on its content, make a decision to do or not do something. You may be able to think of conditionals from real life. For example: if it is a school day you get up early. This has a condition and then an action — If (it is a school day) Then (you get up early). Point out that this does not confirm what will happen if it isn't a school day. In other words, it doesn't say that you don't get up early, only that if it is a school day you do. We are going to program Picoh to run a maths quiz and respond in different ways depending on the answer given.

Teacher Input

Code using ask and wait with the answer block

Let's start by programming Picoh to receive an input from the computer keyboard. Show the program and ask students to predict what will happen when the green flag is clicked. Ask students to try the code themselves. Can they explain how it works? Do they recognise the shape of the reporter block for `answer`? It is similar to the ones used in the previous lesson on time, but instead of reporting time or date, it reports the text that the user has typed in.

When the green flag is clicked the `ask and wait` block will speak the words 'What's your name?', then wait for text to be typed in and the enter key pressed. Then Picoh will say 'Hello!', and because the `answer` block has been dropped into the say block it will speak the text that has been typed in. Highlight that it doesn't have to be letters — if you type in an integer, it will also speak that. Remind children that this code simply enters data from the keyboard into the computer. A conditional is needed to decide what to do with the information.

Diagram of how the if-then block works

We are going to use the `if-then` block. Show the diagram that explains how this works.

Quiz code using an if-then conditional

Can they predict what will happen when the green flag is clicked and the number 4 is entered? How about if the number 10 is entered, or the word cat? Ask children what the green operator block does in this program, and why it is needed. Run the code and try entering different values. Did it do what you expected?

Many if statements covering each possible answer

Currently any answer that is not 4 will get no response. One way to give a response when a wrong answer is entered would be to have an if statement for every possible other answer. As you can see, trying to cover even a few possible answers makes the program very long, and it would be impossible to cover every answer this way.

Code using an if-then-else conditional

Instead we can use an `if-then-else` conditional, so that any answer other than the one that is true will result in running the instructions in the else part of the block.

Activities

1.

Ask children to modify the code to make Picoh ask a different question and give a different response.

2.

Can you modify the program created in Activity 1 to use an if-then-else conditional so that it gives a different response when the wrong answer is entered?

Example if-then-else quiz code
3.

Can you write a program that makes Picoh ask a sequence of questions and give a response to each based on the answer entered?

Code asking a sequence of questions

Extension

What if we wanted to ask a question with a number of possible correct answers? For example, 'Can you name one of Bart Simpson's sisters?' — the answer could be either Lisa or Maggie. We could use the OR operator block as the condition. The OR operator gives a True response if the answer is either one. Can you add a question that has several possible answers?

Code using the OR operator block

How about a question with an answer that must include a number of different answers? For example, 'Can you name both of Bart Simpson's sisters?' — the answer must include both Maggie and Lisa. The `contains` operator block gives a True if the answer has the word Maggie anywhere in it. Combined with the AND operator block it will give a True response if the answer contains Maggie and Lisa. Can you use the contains and AND operator blocks to create a program where you check whether the answer includes both names?

Code combining contains with the AND operator

Share the following program and challenge children to predict what it will do when different answers are entered. Run the program and enter different answers. Can they identify the bug and how to correct it? The program uses AND rather than OR, so it will only give the correct response when all three country names are entered.

Buggy program using AND instead of OR

Plenary

Note: Picoh can also be made to listen for answers using the Ask and Listen block. You will need to have Speech Recognition switched on — click the Search icon, type in Speech Privacy Settings, then move the toggle switch to on.

The Ask and Listen block
Ohbot image

Key Points

  • A conditional performs different actions depending on whether its condition is true or false.
  • `ask and wait` puts the user's reply into the `answer` reporter block.
  • `if-then` runs its instructions only when the condition is true.
  • `if-then-else` handles every other answer in one block, instead of one if per answer.
  • The OR operator is true when either of two conditions is true.
  • `contains` combined with AND checks that an answer includes several things.

Need Help?

If you're stuck on any part of this lesson, check out these resources:

Ohbot