Counter

import microbit

count = 0
microbit.display.show(count)

while True:
    if microbit.button_b.is_pressed():
        count += 1
        microbit.display.show(count)
    sleep(200)

  • Initialize an integer counter variable to 0.
  • Display the initial value on the screen.
  • Run an endless loop.
  • Check for button B press on every iteration.
  • If the button is pressed, increase the counter variable and display the new value.
  • Sleep for 1/5 second.

Key concepts:

  • a counter variable
  • displaying a number on the screen
  • checking for button presses