Week 7 · Unit 7.2: Decomposing problems · 3 lessons
2
Two buttons, two jobs
Computing 7: Unit 7.2, pp. 44–81
GoalTurn a flowchart with a decision into an if statement, and use two buttons in one program.
New words
if statement · a line that runs the indented lines below it only when its question is trueindentation · spaces at the start of a line that show which lines belong togetherwhile True · a loop that repeats the indented lines again and again, foreverbutton_a.is_pressed() · a question that is true while button A is held down
Explanation
A decision diamond in a flowchart becomes an if statement in MicroPython. The question is button_a.is_pressed(), and the line must end with a colon. The lines that happen when the answer is yes are indented by four spaces. Python uses the indentation to know which lines belong to the if. A program that checks the button only once would miss a press that comes later. So we put everything inside while True:, which repeats its indented lines forever, like the forever block in a block language. Button B has its own question, button_b.is_pressed(). Each decision in a flowchart gets its own if.
Two buttons mean two if statements, one after the other. This program points an arrow to the right when A is pressed and to the left when B is pressed. The picture stays until the other button is pressed.
a cross is always shown; pressing A also scrolls the letter A
Indentation changes the meaning. In this program the last line is indented to the same level as the if, so it is inside the while loop but not inside the if. The cross is shown again and again, whether or not A is pressed.
Class activity
Be the micro:bit (no computer). A friend holds a paper micro:bit with a grid and buttons A and B. You read this program line by line, like a computer would, and you only do what the lines say. Your friend taps A or B whenever they like. Draw on the grid what appears each time. Then swap roles and let your friend write the program with one if for each button. If you have a computer or tablet, an adult can help you try a two-button program.
Practice
1
Write the MicroPython program for this flowchart.
Program (each → is 4 spaces of indentation): 1 from microbit import * | 2 while True: | 3 → if button_a.is_pressed(): | 4 → → display.scroll("Home") | 5 → → display.show(Image.HOUSE)
2
Look at the program. First A is pressed and released. Then B is pressed and released. What does the board show after each press?
After A: the letter 1 is scrolled, then the board is blank. After B: the letter 2 is scrolled, then the board is blank. (Scrolling clears the lights when it ends.)
3
This program has two mistakes. Find them and write the correct lines.
Line 2: while True needs a colon at the end: while True:. Line 4: display.show(Image.HEART) must be indented under the if (8 spaces in total).
4
Why do micro:bit programs usually put their if statements inside while True?
Without the loop the program would check the button once, at the moment it starts, and then end. The loop makes the micro:bit keep asking the question, so a press at any later time is noticed.