Python Classroom
  • Introduction
  • About Python Classroom
  • Python Cloud Options
    • CS50 IDE
      • CS50 IDE Overview
      • CS50 IDE and Python
      • CS50 IDE Debugging
      • CS50 IDE Shortcuts
      • Linux Bash Shell
      • Vim Mode
        • Vim Tutorial
    • CS50 Sandbox
    • PythonAnywhere
    • Repl.it
  • Python Curriculum Map
    • Pedagogy
      • Python Professional Development
      • Teaching Tips
      • Assessment Tips
      • Rubrics
      • Activities
        • Picture Activity
        • Map Activity
        • Crossing the Bridge
        • NIM
        • Mastermind
        • Cards
          • Card Deck Algorithms
          • Sorting Cards
        • River Crossing
          • Jealous Boyfriends
          • Cannibals and Priests
          • Family
          • Police and the Thief
          • Humans and Monkeys
          • Moving Money
        • Crossing the River
        • Traveling Salesperson
        • Logic Problems
        • CIA Crack the Code
        • IQ Test
        • Puzzles
    • AP Computer Science Principles Framework
  • Python Philosphy
    • How to Practice Python
  • microbit
  • Turtle Graphics
    • Turtle Examples
    • Turtle Activities
    • Turtle Maze Problems
    • Turtle Graphics with loops
    • Turtle Snake
    • Turtle Graphics with Conditionals
  • Output
    • Output Examples
    • Output Mistakes
  • Variables
    • Variable Data Type Examples
    • Variable Role Examples
    • Variables Mistakes
    • Variables Problems
  • Math
    • Math Examples
    • Math Mistakes
    • Math Problems
    • Math Self Check
  • Input
    • Input Examples
    • Input Mistakes
    • Input Problems
  • Decisions
    • if
      • if Examples
      • if Mistakes
      • if Problems
    • if else
      • if else Examples
      • if else Problems
      • if / if else Problems
    • if elif else
      • if elif else Examples
      • if elif else Problems
    • nested if
      • nested if Examples
      • nested if Problems
    • Logical Operators
      • Logical Operators Examples
    • Adventure Game Project
  • Loops
    • while loop - count up
      • Examples
      • Problems
    • while loop - countdown
      • Examples
      • Problems
    • while loop - sentinel value
      • Problems
    • while loop - sentinel menu
      • Problems
    • for loop - range (one argument)
      • Examples
      • Problems
    • for loop - range (two arguments)
      • Problems
    • for loop - range (three arguments)
      • Problems
  • Lists
    • Lists - Numbers
      • Problems
    • Lists - Strings
      • Problems
      • Shopping Project
  • Dictionaries
  • String Methods
  • Functions
    • Variable Scope
    • Functions - no parameters
    • Functions - one parameter
    • Functions - return
    • Functions - lists
  • Files
  • Classes
    • Inheritance
  • Python Projects
    • Adventure Game
    • Restaurant Project
    • Trivia Game
    • Family Tree Project
  • Raspberry Pi
    • Raspberry Pi Models
    • Raspberry Pi Setup
  • Roblox
  • Glossary
Powered by GitBook
On this page
  • Python Editor for micro:bit
  • Button Press
  • Detect Movement
  • Random Numbers
  • Gestures
  • Magic-8
  • Built-in Images

Was this helpful?

microbit

PreviousHow to Practice PythonNextTurtle Graphics

Last updated 5 years ago

Was this helpful?

Python Editor for micro:bit

Button Press

from microbit import *

while True:
    if button_a.is_pressed() and button_b.is_pressed():
        display.scroll("AB")
    elif button_a.is_pressed():
        display.scroll("A")
    elif button_b.is_pressed():
        display.scroll("B")
    sleep(100)

Detect Movement

from microbit import *

while True:
    reading = accelerometer.get_x()
    if reading > 20:
        display.show("R")
    elif reading < -20:
        display.show("L")
    else:
        display.show("-")

Random Numbers

from microbit import *
import random

display.show(str(random.randint(1, 6)))

Gestures

MicroPython is able to recognize the following gestures: up, down, left, right, face up, face down, freefall, 3g, 6g, 8g, shake. Gestures are always represented as strings. While most of the names should be obvious, the 3g, 6g and 8g gestures apply when the device encounters these levels of g-force (like when an astronaut is launched into space).

from microbit import *

while True:
    gesture = accelerometer.current_gesture()
    if gesture == "face up":
        display.show(Image.HAPPY)
    else:
        display.show(Image.ANGRY)

Magic-8

A Magic-8 ball is a toy first invented in the 1950s. The idea is to ask it a yes/no question, shake it and wait for it to reveal the truth.

from microbit import *
import random

answers = [
    "It is certain",
    "It is decidedly so",
    "Without a doubt",
    "Yes, definitely",
    "You may rely on it",
    "As I see it, yes",
    "Most likely",
    "Outlook good",
    "Yes",
    "Signs point to yes",
    "Reply hazy try again",
    "Ask again later",
    "Better not tell you now",
    "Cannot predict now",
    "Concentrate and ask again",
    "Don't count on it"
    "My reply is no",
    "My sources say no",
    "Outlook not so good",
    "Very doubtful",
]

while True:
    display.show("8")
    if accelerometer.was_gesture("shake"):
        display.clear()
        sleep(1000)
        display.scroll(random.choice(answers))

Built-in Images

  • Image.HAPPY

  • Image.HEART_SMALL

  • Image.HAPPY

  • Image.SMILE

  • Image.SAD

  • Image.CONFUSED

  • Image.ANGRY

  • Image.ASLEEP

  • Image.SURPRISED

  • Image.SILLY

  • Image.FABULOUS

  • Image.MEH

  • Image.YES

  • Image.NO

  • Image.CLOCK12

  • Image.CLOCK11

  • Image.CLOCK10

  • Image.CLOCK9

  • Image.CLOCK8

  • Image.CLOCK7

  • Image.CLOCK6

  • Image.CLOCK5

  • Image.CLOCK4

  • Image.CLOCK3

  • Image.CLOCK2

  • Image.CLOCK1

  • Image.ARROW_N

  • Image.ARROW_NE

  • Image.ARROW_E

  • Image.ARROW_SE

  • Image.ARROW_S

  • Image.ARROW_SW

  • Image.ARROW_W

  • Image.ARROW_NW

  • Image.TRIANGLE

  • Image.TRIANGLE_LEFT

  • Image.CHESSBOARD

  • Image.DIAMOND

  • Image.DIAMOND_SMALL

  • Image.SQUARE

  • Image.SQUARE_SMALL

  • Image.RABBIT

  • Image.COW

  • Image.MUSIC_CROTCHET

  • Image.MUSIC_QUAVER

  • Image.MUSIC_QUAVERS

  • Image.PITCHFORK

  • Image.XMAS

  • Image.PACMAN

  • Image.TARGET

  • Image.TSHIRT

  • Image.ROLLERSKATE

  • Image.DUCK

  • Image.HOUSE

  • Image.TORTOISE

  • Image.BUTTERFLY

  • Image.STICKFIGURE

  • Image.GHOST

  • Image.SWORD

  • Image.GIRAFFE

  • Image.SKULL

  • Image.UMBRELLA

  • Image.SNAKE

Python Editor for micro:bit
Logo