10. Buttons and selection
10.1. Button is pressed
.is_pressed
method is used to detect button pressing at the moment the running code gets to that point.- button_a.is_pressed()
- returns
True
if the A-button is being pressed orFalse
if not.
- button_b.is_pressed()
- returns
True
if the B-button is being pressed orFalse
if not.
from microbit import *
while True:
display.scroll(button_a.is_pressed(), delay=60)
sleep(1000)
Tasks
Edit the code to indicate whether the B-button is being pressed or not.
Edit the code to indicate whether the B-button is being pressed or not.
from microbit import *
while True:
display.scroll(button_b.is_pressed(), delay=60)
sleep(1000)
10.2. Button was pressed
.was_pressed
method is used to detect button pressing since the last check or since the device started..was_pressed
method will clear the press state so that the button must be pressed again before this method will return True again.- button_a.was_pressed()
- returns
True
if the A-button was pressed since the last check, orFalse
if not.
- button_b.was_pressed()
- returns
True
if the B-button was pressed since the last check, orFalse
if not.
from microbit import *
while True:
display.scroll(button_b.was_pressed(), delay=60)
sleep(1000)
Tasks
Edit the code to indicate whether the A-button was pressed or not.
Edit the code to indicate whether the A-button was pressed or not.
from microbit import *
while True:
display.scroll(button_a.was_pressed(), delay=60)
sleep(1000)
10.3. Selection
if
and elif
statements.if
, elif
and else
provide choices or branches in the code.:
.if
and elif
test a condition that returns True
or False
. Their indented code block runs if the condition is True.elif
can be used to provide more choices.else
block does not have a condition.else
block only runs if all the previous conditions were False
.10.4. if
if
requires a condition that returns True
or False
.from microbit import *
num = 0
while True:
if num == 5:
display.scroll(num)
num = num + 1
sleep(200)
from microbit import *
while True:
if button_a.is_pressed() == True:
display.scroll("A")
sleep(200)
button_a.is_pressed()
returns a boolean, there is no need to use == True
.if button_a.is_pressed():
becomes if True:
when the A-button is pressed.if button_a.is_pressed():
becomes if False:
when the A-button is not pressed.from microbit import *
while True:
if button_a.is_pressed():
display.scroll("A")
sleep(200)
Tasks
Edit the code to scroll your name when the A-button is pressed.
Edit the code to display a happy face when the A-button is pressed.
Edit the code to scroll your age when the B-button is pressed.
Edit the code to display a sad face when the B-button is pressed.
Edit the code to scroll your name when the A-button is pressed.
from microbit import *
while True:
if button_a.is_pressed():
display.scroll("name")
sleep(200)
Edit the code to display a happy face when the A-button is pressed.
from microbit import *
while True:
if button_a.is_pressed():
display.show(Image.HAPPY)
sleep(200)
Edit the code to scroll your age when the B-button is pressed.
from microbit import *
while True:
if button_b.is_pressed():
display.scroll(12)
sleep(200)
Edit the code to display a sad face when the B-button is pressed.
from microbit import *
while True:
if button_b.is_pressed():
display.show(Image.SAD)
sleep(200)
10.5. if - else
else
block does not have a condition.else
block only runs if all the previous conditions were False
.from microbit import *
while True:
if button_a.is_pressed():
display.show("A")
else:
display.show("X")
sleep(200)
Tasks
Edit the code to scroll your name when the A-button is pressed and to show “?” when nothing is pressed.
Edit the code to display a happy face when the A-button is pressed and a sad face when nothing is pressed.
Edit the code to display a sad face when the B-button is pressed and a confused face when nothing is pressed.
Edit the code to scroll your name when the A-button is pressed and to show “?” when nothing is pressed.
from microbit import *
while True:
if button_a.is_pressed():
display.scroll("name")
else:
display.show("?")
sleep(200)
Edit the code to display a happy face when the A-button is pressed and a sad face when nothing is pressed.
from microbit import *
while True:
if button_a.is_pressed():
display.show(Image.HAPPY)
else:
display.show(Image.SAD)
sleep(200)
Edit the code to display a sad face when the B-button is pressed and a confused face when nothing is pressed.
from microbit import *
while True:
if button_b.is_pressed():
display.show(Image.SAD)
else:
display.show(Image.CONFUSED)
sleep(200)
10.6. if - elif
elif
can be used to provide another choice by testing to see if its condition is True.from microbit import *
while True:
if button_a.is_pressed():
display.show("A")
elif button_b.is_pressed():
display.show("B")
sleep(200)
Tasks
Edit the code to scroll your name when the A-button is pressed and your tutor group when the B-button is pressed.
Edit the code to display a happy face when the A-button is pressed and a sad face when the B-button is pressed.
Edit the code to scroll your name when the A-button is pressed and your tutor group when the B-button is pressed.
from microbit import *
while True:
if button_a.is_pressed():
display.scroll("name")
elif button_b.is_pressed():
display.scroll("TG")
sleep(200)
Edit the code to display a happy face when the A-button is pressed and a sad face when the B-button is pressed.
from microbit import *
while True:
if button_a.is_pressed():
display.show(Image.HAPPY)
elif button_b.is_pressed():
display.show(Image.SAD)
sleep(200)
10.7. If - elif - else
if
, elif
and else
together provides 3 branches in the code.from microbit import *
while True:
if button_a.is_pressed():
display.show("A")
elif button_b.is_pressed():
display.show("B")
else:
display.show("X")
sleep(200)
Tasks
Edit the code to scroll your name when the A-button is pressed and your school house when the B-button is pressed and your Tutor group when nothing is pressed.
Edit the code to display a happy face when the A-button is pressed and a sad face when the B-button is pressed and a confused face when nothing is pressed.
Edit the code to scroll your name when the A-button is pressed and your school house when the B-button is pressed and your Tutor group when nothing is pressed.
from microbit import *
while True:
if button_a.is_pressed():
display.scroll("name")
elif button_b.is_pressed():
display.scroll("house")
else:
display.scroll("TG")
sleep(200)
Edit the code to display a happy face when the A-button is pressed and a sad face when the B-button is pressed and a confused face when nothing is pressed.
from microbit import *
while True:
if button_a.is_pressed():
display.show(Image.HAPPY)
elif button_b.is_pressed():
display.show(Image.SAD)
else:
display.show(Image.CONFUSED)
sleep(200)
10.8. If - elif - elif - else
if
, two elif
and else
together provides 4 branches in the code.and
requires both conditions to be True for the combined condition to be True.button_a.is_pressed() and button_b.is_pressed()
, will be False.from microbit import *
while True:
if button_a.is_pressed() and button_b.is_pressed():
display.show(Image.ARROW_N)
elif button_a.is_pressed():
display.show(Image.ARROW_W)
elif button_b.is_pressed():
display.show(Image.ARROW_E)
else:
display.show(Image.ARROW_S)
sleep(100)
Tasks
Edit the code to scroll your favourite subject when both buttons are pressed together, your best subject when the A-button is pressed, your favourite sport when the B-button is pressed, and your favourite food when nothing is pressed.
Edit the code to display a giraffe when both buttons are pressed, a duck when the A-button is pressed, a rabbit when the B-button is pressed and a snake when nothing is pressed.
Edit the code to scroll your favourite subject when both buttons are pressed together, your best subject when the A-button is pressed, your favourite sport when the B-button is pressed, and your favourite food when nothing is pressed.
from microbit import *
while True:
if button_a.is_pressed() and button_b.is_pressed():
display.scroll("digistem")
elif button_a.is_pressed():
display.scroll("maths")
elif button_b.is_pressed():
display.scroll("table tennis")
else:
display.scroll("chicken 5 spice")
sleep(100)
Edit the code to display a giraffe when both buttons are pressed, a duck when the A-button is pressed, a rabbit when the B-button is pressed and a snake when nothing is pressed.
from microbit import *
while True:
if button_a.is_pressed() and button_b.is_pressed():
display.show(Image.GIRAFFE)
elif button_a.is_pressed():
display.show(Image.DUCK)
elif button_b.is_pressed():
display.show(Image.RABBIT)
else:
display.show(Image.SNAKE)
sleep(100)
10.9. Changing values with A and B-buttons
delay_time
variable is increased in steps of 10 by the A-button and decreased in steps of 10 by the B-button. Finally, text is scrolled with a delay of delay_time
.from microbit import *
delay_time = 80
while True:
if button_a.is_pressed():
delay_time += 10
elif button_b.is_pressed():
delay_time -= 10
else:
sleep(100)
display.scroll("ABC", delay=delay_time)
delay_time
, can be restricted to a set range of values using the min and max functions.delay_time = min(400, delay_time + 10)
prevents the delay_time
from going above 400.delay_time = max(50, delay_time - 10)
prevents the delay_time
from going below 50.from microbit import *
delay_time = 80
while True:
if button_a.is_pressed():
delay_time = min(400, delay_time + 10)
elif button_b.is_pressed():
delay_time = max(50, delay_time - 10)
else:
sleep(100)
display.scroll("ABC", delay=delay_time)
Tasks
Edit the code to adjust the scroll delay in steps of 25.
Write code to alter a
guess_number
variable in steps of 1 by the buttons. Start the number at 5 and limit it to a minimum of 1 and a maximum of 9. Use was_pressed.Write code to alter a
guess_number
variable in steps of 1 by the buttons. Use both buttons to set the number and show it. Start the number at 5 and limit it to a minimum of 1 and a maximum of 9.
Edit the code to adjust the scroll delay in steps of 25.
from microbit import *
delay_time = 80
while True:
if button_a.is_pressed():
delay_time += 25
elif button_b.is_pressed():
delay_time -= 25
else:
sleep(100)
display.scroll("ABC", delay=delay_time)
Write code to alter a guess_number
variable in steps of 1 by the buttons. Start the number at 5 and limit it to a minimum of 1 and a maximum of 9. Use was_pressed.
from microbit import *
guess_number = 5
step = 1
while True:
if button_a.was_pressed():
# limit max to 9
guess_number = min(9, guess_number + step)
elif button_b.was_pressed():
# limit minimum to 1
guess_number = max(1, guess_number - step)
else:
sleep(100)
display.show(guess_number)
Write code to alter a guess_number
variable in steps of 1 by the buttons. Use both buttons to set the number and scroll it. Start the number at 5 and limit it to a minimum of 1 and a maximum of 9.
from microbit import *
guess_number = 5
while True:
if button_a.is_pressed() and button_b.is_pressed():
display.show(guess_number, delay=80)
# now start again
guess_number = 5
if button_a.was_pressed():
guess_number = min(9, guess_number + 1)
elif button_b.was_pressed():
guess_number = max(1, guess_number - 1)
else:
sleep(100)
display.show(guess_number, delay=80)
sleep(200)