4. Buttons and Selection Code Ordering
4.1. Question 1
Rearrange the lines below to create a simple program that continuously checks if Button A is pressed.
If it is pressed, scroll the message “Yes”.
Drag and drop lines into the correct order and click to adjust indentation:
☰
while True:
☰
if button_a.is_pressed():
☰
display.scroll("Yes")
☰
☰
from microbit import *
4.2. Question 2
Put the code snippets in order to build a program that shows the letter “A” when Button A is pressed, or clears the display completely when nobody is touching the board.
Drag and drop lines into the correct order and click to adjust indentation:
☰
while True:
☰
from microbit import *
☰
display.clear()
☰
if button_a.is_pressed():
☰
else:
☰
☰
display.show("A")
4.3. Question 3
Arrange the lines of code to check for a single click event history on Button B.
When a click is detected, show a built-in happy face image.
Drag and drop lines into the correct order and click to adjust indentation:
☰
if button_b.was_pressed():
☰
display.show(Image.HAPPY)
☰
☰
from microbit import *
☰
while True:
4.4. Question 4
A student wants to set up a linked selection chain.
If Button A is down, show “A”. If Button B is down instead, show “B”. Otherwise, keep the screen clear.
Put the blocks in the correct vertical structure.
Drag and drop lines into the correct order and click to adjust indentation:
☰
display.show("A")
☰
from microbit import *
☰
display.clear()
☰
while True:
☰
else:
☰
☰
display.show("B")
☰
elif button_b.is_pressed():
☰
if button_a.is_pressed():
4.5. Question 5
Rearrange the segments to create a protected baseline counter.
When Button A is clicked, use the min function to increase the counter variable by 1 but cap it so it cannot exceed 9.
Drag and drop lines into the correct order and click to adjust indentation:
☰
display.show(guess_number)
☰
from microbit import *
☰
if button_a.was_pressed():
☰
guess_number = min(9, guess_number + 1)
☰
guess_number = 5
☰
☰
while True:
☰
sleep(200)