Learning Intention

  • I can use a list in a program

Introduction

What new skill did we learn last week? (We used the mouse to allow a human to control Picoh and simulate Picoh's response.) What is this type of robotics called? ('Wizard of Oz' robotics.) This week we will be learning another new skill: to incorporate a list into a program. What is a list?

Teacher Input

Creating a list with Data, Make a List

First, we are going to learn how to make a list and then how to input items into the list. There are several different ways you can input items into a list. Method one adds one name at a time using some simple code. First, demonstrate how to create a list using Data, Make a List. Remember to add the tick so you can watch the list being created. Together, write a simple program using the code `add thing to list`.

Adding an item to a list using the ask command

Next, demonstrate method two: how to add an item to the list using the `ask` command.

Typing list items separated by the pipe symbol

Next, we will learn another way of adding items into a list: it is very simple, just type the names in, separated by the pipe symbol |. Check all children can find the pipe on the keyboard (bottom left, next to z). This is very useful if you are making multiple lists or very long lists.

Code generating a two word poem from two lists

Now we can create some projects that use a greater number of items in a list. For example, Picoh can make up a simple poem by randomly generating items in a list. Ask the children to create a list of animals, then a list of adjectives to describe the animals. With some simple code we can generate a two word poem.

Using the contains operator inside an if command

Next, we will develop our list projects further by adding an `if` command to the code. We are going to create a project called 'favourite fruit'. Picoh will ask the user what their 3 or 4 favourite fruits are and then say the answers out loud in turn. Picoh can then respond depending on whether the condition is met, e.g. if the list contains apples, say 'I love apples too', else say 'I can't believe apples are not one of your favourite fruits!'. Demonstrate how to use the hexagonal `contains` operator inside the if command.

Activities

1.

Can you write a program that makes Picoh ask four children to type in their names, adds the names to a list, then selects one at random to win a special prize?

Code adding names to a list and picking one at random
2.

Can you write code to make Picoh say a simple poem by accessing three or more lists? Think of all the word games and word work activities you have completed.

Code building a poem from three lists
3.

Can you write a program to make Picoh ask for three favourite fruits and then read the list out loud, followed by an if command for Picoh to respond if its favourite fruit appears on the list?

Favourite fruit code using contains inside an if command

Extension

Can you write a program to make Picoh react if all three of its favourite fruits appear in the list? This can be done using two AND operator blocks.

Plenary

Take a look at the children's projects.

Questions:

  • Were you able to successfully write the code?
Ohbot image

Key Points

  • Create a list with Data, Make a List, and tick it to watch it fill up.
  • `add thing to list` adds one item at a time.
  • Items can also be added from an `ask` command, or typed separated by the pipe symbol.
  • Picking a random item from a list is the basis of poem and prize-draw projects.
  • The `contains` operator tests whether a list holds a particular item.

Need Help?

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

Ohbot