Step Zero: Get MicroPython

On Google Chrome, find the app ‘MicroPython’ in the Web Store - it’s free. This will allow you to write Python scripts and upload them to the Microbit. If you’re reading this online, click the link to be put straight through to it.

Step One: Write an ‘8’ to the screen

In the Microbit app, write the following code:

from microbit import *
import random

while True:
    sleep(1000)
    display.show("8")

Upload the code to the Microbit and run it. What happens when you shake the Microbit?

Step Two: Shake it!

Add the following code to what you have already written, inside the ‘While True:’

if accelerometer.was_gesture("shake"):
    display.clear()
    sleep(1000)
    display.show("!")

Upload the code to the Microbit and run it. What happens when you shake the Microbit?

Step Three: Write some responses

We need to list our responses, and in Python we do that in an array variable. An ‘array’ is just a fancy word for list. Write this code before the While True:

answers = [
    "Yes",
    "No",
    "Maybe",
    "Yes, definitely"
]

Step Four: Pick a random response

To make the Magic 8-Ball pick a random response, we can get it to pick a random answer using random.choice.

sleep(1000)
display.scroll(random.choice(answers))

When you shake the Microbit, this code will make the BBC microbit wait a second and then show a random answer to the question you put to it.

Full Code

from microbit import *
import random

answers = ["Yes", "No", "Maybe", "Try again", "It doesn't look good", "Don't count on it"]

while True:
    display.show("8")
    if accelerometer.was_gesture("shake"):
        display.clear()
        sleep(1000)
        display.scroll(random.choice(answers))

More BBC Microbit projects will be available soon


Creative Commons License This microbit worksheet 'Magic 8-Ball' from CoderDojo York is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.
No commercial use of our material is permitted