```html Micro:bit Python Lesson — Buttons A and B

Micro:bit Python Lesson — Buttons A and B

Detect button presses with button_a.is_pressed() / button_b.is_pressed() and show different messages.

Objective

Learn to detect button presses using Python on the micro:bit and display different messages when Button A or Button B is pressed.

Background

Your micro:bit has two built-in buttons: A (left) and B (right). In Python:

Step-by-Step Instructions

  1. Open the micro:bit Python editor.
  2. Delete any starter code.
  3. Paste the following program and flash it to your micro:bit.
  4. You MUST NOT use any of the images below.
  5. You must find you own.
Python (micro:bit)
from microbit import *

while True:
    if button_a.is_pressed():
        display.show("A")
    elif button_b.is_pressed():
        display.show("B")
    else:
        display.clear()

How it works: The loop runs forever. If A is pressed, show “A”; else if B is pressed, show “B”; otherwise clear the display.

Your Task

  1. Change Button A to show a happy face.
  2. Change Button B to show a sad face.
  3. Add a third case: if both A and B are pressed at the same time, show a surprised face.

Hint: use Image.HAPPY, Image.SAD, and Image.SURPRISED.

Sample Solution (for checking)

Python (micro:bit)
from microbit import *

while True:
    if button_a.is_pressed() and button_b.is_pressed():
        display.show(Image.SURPRISED)
    elif button_a.is_pressed():
        display.show(Image.HAPPY)
    elif button_b.is_pressed():
        display.show(Image.SAD)
    else:
        display.clear()

Challenge Extension This is REQUIRED

Swap faces for words: have Button A scroll YES and Button B scroll NO.

Python (micro:bit)
from microbit import *

while True:
    if button_a.is_pressed():
        display.scroll("YES")
    elif button_b.is_pressed():
        display.scroll("NO")
    else:
        display.clear()

The name of the file will be:
PX_lastname_AB_Button

Files to save on Google Drive under your class and submit to Google Classroom

  1. PX_lastname_AB_Button.png — Screenshot inside Python editor showing your code.
  2. PX_lastname_AB_Button.py — Your Python source file.
  3. PX_lastname_AB_Button.txt — Plain-text copy of your code.
  4. PX_lastname_AB_Button.hex — Hex file to run on the physical micro:bit.
  5. PX_lastname_AB_Button.mp4 — Short demo video of the program running on the board.

Submission requirements

```