Appendix C
Python Recipes
THIS APPENDIX CONTAINS complete code listings for the programs detailed in Chapter 7, ‘Python’, Chapter 8, ‘The Wireless BBC micro:bit’, Chapter 9, ‘The BBC micro:bit and the Raspberry Pi’, and Chapter 10, ‘Building Circuits’, presented without comments to make them easier to type in and compare against. When a line of code would extend past the border of the page, a ↩ symbol is printed. When you see this symbol, continue to type the code without pressing the Enter or Return keys. If you’re not sure how a line of code should be entered, visit the website at www.wiley.com/go/bbcmicrobituserguide
to download plain-text versions of each program; these can then be used for reference or even simply copy and pasted directly into the editors.
Chapter 7: Hello, World! (Non-looping)
from microbit import *display.scroll('Hello, World!')
Chapter 7: Hello, World! (Looping)
from microbit import *while True: display.scroll('Hello, World!')
Chapter 7: Button Inputs (Single Button)
from microbit import *while True: if button_a.is_pressed(): display.show(Image.HAPPY)
Chapter 7: Button Inputs (Two Buttons)
from microbit import *while True: if button_a.is_pressed(): display.show(Image.HAPPY) if button_b.is_pressed(): display.show(Image.SAD)
Chapter 7: Touch Inputs
from microbit import *touches = 0while True: if pin0.is_touched(): touches += 1 display.scroll(str(touches))
Chapter 7: Temperature Sensor (No Formatting)
from microbit import *while True: display.scroll(str(temperature())) ...
Get The Official BBC Micro:bit User Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.