Learning Intention

  • I can use a variable to make Picoh count

Introduction

Creating a variable called Count

Explain that we are going to make Picoh count. For this we are going to be creating a program that uses a variable. You might want to discuss the use of a variable to keep and alter a score in a computer game. First, we will use Make a Variable from the Data palette to create a variable called 'Count'. Make sure there is a tick in the box next to Count — this will display the value of the variable at the top left of the code area.

Teacher Input

Code setting and changing the Count variable

To start with we need to set the value of the variable. We can start our program with a `when green flag clicked` instruction. First, we need a `set Count to 0` instruction — this means we will start counting at zero. Next, we need to make a loop where we continually increase the value of Count by 1, using a `forever` instruction and a `change Count by 1` instruction. Test the code by clicking the green flag. Once successful, File, Save As - countinones. We can now use our first variable program as a skeleton to make other counting programs.

Activities

1.

Can you write some code to make Picoh count in twos?

Code counting in twos
2.

Can you write some code to make Picoh count in fives?

3.

Can you write some code to make Picoh count in tens?

4.

Can you write some code to make Picoh count in a 3 digit number?

5.

Can you write some code to make Picoh count in a 4 digit number?

Extension

Can you change the number that Picoh starts counting from? Can you make Picoh count in ones starting from 20? If Picoh wants to start counting from 20, which part of the code do you think we have to change? (`set Count to`.) Remember to tick the box next to the Count variable on the Data palette so you can watch the counting. File, Save As - countfrom20

Code starting the count from 20

Can you make Picoh count in twos starting from 30?

Can you make Picoh count in fives starting from 25?

Can you make Picoh count in tens starting from 100?

Can you make Picoh count in 125s starting from 1000?

Plenary

Encourage children to share their programs with the class and discuss ones that don't work, work partially, or have used a different algorithm to reach the same outcome. Encourage children to talk through how they work block by block and work as a class to debug a program to improve it.

Ohbot image

Key Points

  • A variable stores a value that your program can read and change as it runs.
  • Create one with Make a Variable on the Data palette.
  • `set [Count] to 0` gives the variable its starting value.
  • `change [Count] by 1` increases it each time round the loop.
  • Change the step to count in twos, fives or tens.
  • Tick the variable on the Data palette to watch its value on screen.

Need Help?

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

Ohbot