GoalTrace a micro:bit program that uses a variable, a random number and if … then … else to show different outputs.
New words
micro:bit · a small programmable board with 25 LED lights and two buttonsLED · a small light that can be switched on or offsimulator · a program that shows on a screen what a micro:bit would doicon · a small picture that the lights show, such as a heart
Explanation
A micro:bit is a small board that you program with blocks. It has 25 LED lights, two buttons called A and B, and sensors. We can test a program on a simulator on a screen before we use the real board. Blocks such as ‘on button A pressed’ and ‘on shake’ start a program. The ‘set variable to’ block stores a value, and ‘pick random’ gives a whole number between two limits. The ‘if … then … else’ block is in the Logic group. ‘Show icon’, ‘show number’ and ‘show string’ are output blocks in the Basic group. An adult should help with the real board and its batteries.
Worked examples
A playground game picker. When buttons A and B are pressed together, the board picks 1, 2 or 3. If the number equals 3 it shows the word Tag, otherwise it shows Hop. So Hop comes up for two of the three numbers.
A lucky dice. When button B is pressed, the board rolls a number from 1 to 6. A 6 shows a happy face. Every other number is shown as a number.
Roll
roll = 6?
Output
4
false
shows 4
6
true
shows a happy face
2
false
shows 2
We cannot know the random numbers in advance, but we can trace the program for some made-up rolls.
Class activity
Be the micro:bit (pairs). Draw a board with a 5 × 5 grid for the lights and two buttons A and B on paper. Player A is the board. Player B holds a die and says ‘press B’. Then A rolls the die (the random block), writes the number in a cup labelled roll, checks ‘roll = 6?’ and either draws a smile on the grid or writes the number. Play 12 presses. How many smiles did you get? Predict first how many you expect in 12 presses. If you have a computer or tablet, build the lucky dice in a simulator.
Practice
1
In 4 button presses, the random numbers are 3, 2, 1 and 2. What does the board show each time?
Press 1: diamond, press 2: heart, press 3: diamond, press 4: heart. Only a 2 shows the heart; 1 and 3 both go to the else part.
2
A program uses ‘pick random 1 to 3’ and shows a star only when the variable equals 3. How many possible results are there? On how many of them is the star shown?
There are 3 possible results: 1, 2 and 3. The star is shown for just 1 of them.
3
The program must show this smile when tries = 1, and a sad face when tries is anything else. It starts when button B is pressed and picks tries from 1 to 3. Write the blocks in order.
on button B pressed: set tries to pick random 1 to 3; if tries = 1 then show icon happy, else show icon sad.
4
A micro:bit program does nothing when button A is pressed. Name two things to check.
Check that the blocks are inside the ‘on button A pressed’ block, and that the program starts on button A and not on button B. Also check that the simulator is running.