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.
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.
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. e.g if button_a.is_pressed():
.elif
blocks 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 *
while True:
if button_a.is_pressed():
display.show("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 via logical and
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.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)