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:
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:’
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:
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.
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
More BBC Microbit projects will be available soon
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