Skip to content

Activity

Activity picker

Beginner | MakeCode, Python | Buttons, LED display | Randomisation, Selection, Variables

Step 1: Make it

What is it?

Finding it hard to decide or agree on what to do? Let this micro:bit program choose for you!

These two videos show you what you'll make and how to code it:

Introduction

Coding guide

How it works

  • Pressing button A makes your micro:bit choose a random number between 1 and 6.
  • It stores the number in a variable called random_number.
  • The program tests the random number using selection. Depending on the number, different activities are shown.
  • It doesn't specifically test if the random number is 6 - why is this?

What you need

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

Step 2: Code it

1from microbit import * 2import random 3 4while True: 5 if button_a.is_pressed(): 6 random_number = random.randint(1, 6) 7 if random_number == 1: 8 display.scroll('PE with Joe') 9 elif random_number == 2: 10 display.scroll('watch a movie') 11 elif random_number == 3: 12 display.scroll('play a board game') 13 elif random_number == 4: 14 display.scroll('tidy our rooms') 15 elif random_number == 5: 16 display.scroll('play a card game') 17 else: 18 display.scroll('learn a song')

Step 3: Improve it

  • Customise it by putting your own activities in the code.
  • Add more activities.
  • How could you make it less likely that it tells you to tidy your room?