Skip to content

Activity

Activity array

Intermediate | MakeCode, Python | Buttons, LED display | Data structures, Randomisation, Variables

Step 1: Make it

What is it?

Finding hard to decide or agree on what to do? Use arrays to create a micro:bit program that chooses for you!

micro:bit with different activity icons

How it works

  • Your micro:bit stores the list of your possible activities in a list (or array) called 'options'. Arrays are really useful ways of storing data in lists.
  • When you press button A it chooses an item from the list at random and shows it on the LED display.
  • Using an array makes it really easy to modify the code to add more options to the list.
  • Because the code measures how long the array is, you never need to modify the random number code, you can just add things and take things away from the list.
  • It picks a random number and stores it in a variable called 'choice'. The number will be between 0 and one less than the length of the array because computers usually start counting items in arrays from 0. 'PE with Joe' is item number 0 in the list, the last item 'bake a cake' is item number 5, but the array is 6 items long.

What you need

  • micro:bit (or MakeCode simulator)
  • MakeCode editor
  • battery pack (optional)

Step 2: Code it

1from microbit import *
2import random
3
4options = ['PE with Joe', 
5           'watch a movie',
6           'play a board game',
7           'tidy our rooms',
8           'learn a song',
9           'bake a cake']
10
11while True:
12    if button_a.is_pressed():
13        choice = random.randint(0, len(options)-1)
14        display.scroll(options[choice])

Step 3: Improve it

  • Customise it by putting your own activities in the code.
  • How could you make it more likely to pick your favourite activity?
  • Try writing the same program in Python.