# data[].attributes.body
for storage

# data[].attributes.body
'If-else': once only

'For': repeat for said number of times

'While': do until said number

# data[].attributes.body
Variable type of true or false
bool(True)

isnumeric = number

# data[].attributes.body
"for loop" -> has a variable (usually, i) defines variable itself

"while loop" -> does not define its variable itself -> outside the loop, you have to define it itself

# data[].attributes.body
from microbit import *

while True:
if button_a.is_pressed():
display.show(Image.UMBRELLA)
if button_b.is_pressed():
display.show(Image.HAPPY)

# data[].attributes.body
from microbit import *
while True:
if button_a.is_pressed():
display.show(Image.DUCK)
else:
display.clear()

# data[].attributes.body
from microbit import *

while True:
if temperature() < 20:
display.show(Image.SAD)
else:
display.show(Image.HAPPY)

# data[].attributes.body
from microbit import *

while True:
if accelerometer.get_y() > 200:
display.show(Image.SURPRISED)
else:
display.show(Image.ASLEEP)

# data[].attributes.body
from microbit import *
import radio

radio.on()
clear_signal = 'clear'

while True:
message = radio.receive()
if message and message.startswith('brillig:'):
if message == 'brillig:' + clear_signal:
display.clear()
else:
display.scroll(message)

# data[].attributes.body
from microbit import *
import radio

radio.on()
while True:
message = radio.receive()
if message and message.startswith('brillig:'):
display.scroll(message)

# data[].attributes.body
from microbit import *

showing = False
while True:
if button_a.was_pressed():
if showing:
display.clear()
showing = False
else:
display.show(Image.HEART)
showing = True

# data[].attributes.body
from microbit import *
import radio

radio.on()
radio.config(channel=20)

# data[].attributes.body
accelerometer.get_x()
accelerometer.get_y()
accelerometer.get_z()

# data[].attributes.body
from microbit import *
x = 0
while True:
display.set_pixel(x,0,9)
if button_a.was_pressed():
display.clear()
if x == 4:
x = 0
else:
x = x + 1

# data[].attributes.body
from microbit import *

my_image = Image('90909:'
'90009:'
'90009:'
'90009:'
'90909:')
display.show(my_image)

# data[].attributes.body
from microbit import *
age = 12
while True:
display.scroll(str(age))
age = age + 5

# data[].attributes.body
from microbit import *
import music

start = running_time()
music.play(music.ENTERTAINER)
end = running_time()
duration = (end - start) / 1000
display.scroll(str(duration))
display.scroll("seconds")

# data[].attributes.body
from microbit import *

answer = 0
while True:
if button_a.is_pressed() == True:
answer = answer + 2
display.scroll(str(answer))

# data[].attributes.body
from microbit import *
import radio

radio.on()
while True:
message = radio.receive()
if message == 'smile':
display.show(Image.HAPPY)
elif message == 'frown':
display.show(Image.SAD)

# data[].attributes.body
from microbit import *
import radio

radio.on()
while True:
message = radio.receive()
if message == 'smile':
display.show(Image.HAPPY)

# data[].attributes.body
from microbit import *
import radio

radio.on()
while True:
message = radio.receive()
if message:
display.scroll(message)

# data[].attributes.body
from microbit import *
import radio

radio.on()
radio.send('Open sesame')

# data[].attributes.body
from microbit import *

start = running_time()
while True:
display.show(Image.YES)

if button_b.is_pressed():
time = running_time() - start
display.scroll(str(time))
display.scroll('ms')

# data[].attributes.body
burpee_count = 0
display.show(Image.YES)
sleep(1000)
display.clear()
while True:
if button_a.was_pressed():
burpee_count = burpee_count + 1
display.show(Image.HEART)
sleep(100)
display.clear()
if button_b.was_pressed():
display.scroll(str(burpee_count)) # convert integer to string
burpee_count = 0
display.show(Image.YES)
sleep(1000)
display.clear()

# data[].attributes.body
from microbit import *

burpee_count = 0
display.show(Image.YES)
sleep(1000)
display.clear()
while True:
if button_a.was_pressed():
burpee_count = burpee_count + 1
display.show(Image.HEART)
sleep(100)
display.clear()
if button_b.was_pressed():
display.scroll(str(burpee_count))
burpee_count = 0
display.show(Image.YES)
sleep(1000)
display.clear()

# data[].attributes.body
from microbit import *
name = "micro:"
display.scroll(name)
name = "bit"
display.scroll(name)

# data[].attributes.body
from microbit import *
while True:
display.show(Image.ASLEEP)
if button_a.is_pressed():
while accelerometer.was_gesture("shake") == False:
display.show(Image.MEH)
display.show(Image.SURPRISED)
sleep(1000)

# data[].attributes.body
If you want to make sure that your condition is checked at the start of each loop, you must use was_pressed().

This is because is_pressed() is checked only once, and if you aren't pressing the button at that exact same microsecond, your program is going to miss it!

# data[].attributes.body
from microbit import *
while True:
while button_a.is_pressed() == False:
display.show(Image.DUCK)
display.show(Image.COW)
sleep(2000)

# data[].attributes.body
from microbit import *
import music

while True:
while button_a.is_pressed() == True:
music.play(music.BA_DING)

# data[].attributes.body
up
down
left
right
face up
face down
freefall
3g
6g
8g
shake

# data[].attributes.body
from microbit import *

while True:
if accelerometer.was_gesture("shake"):
display.show(Image.HAPPY)
sleep(500)
display.clear()

# data[].attributes.body
from microbit import *
import music
while True:
display.show(Image.MUSIC_QUAVER)
if button_a.was_pressed():
music.play(music.ENTERTAINER)

# data[].attributes.body
from microbit import *
import music
start = running_time()
music.play(music.ENTERTAINER)
end = running_time()
duration = end-start
display.scroll(str(duration))

# data[].attributes.body
goal = Image.ARROW_W
goal = Image.ARROW_W
while True:
if button_a.is_pressed():
display.show(goal)
sleep(500)
display.clear()
if button_b.was_pressed():
goal = Image.ARROW_E
display.show(goal)

# data[].attributes.body
from microbit import *
cheer = "We are the champions!"
display.scroll(cheer)
display.scroll(cheer)
display.scroll("That's right!")
display.scroll(cheer)

# data[].attributes.body
from microbit import *
while True:
if button_a.is_pressed() and button_b.is_pressed():
display.show(Image.ARROW_N)
elif button_b.is_pressed():
display.show(Image.ARROW_E)
elif button_a.is_pressed():
display.show(Image.ARROW_W)
else:
display.clear()

# data[].attributes.body
while True:
if button_a.is_pressed() and button_b.is_pressed():
display.show(Image.SNAKE)
else:
display.clear()

# data[].attributes.body
music.play(music.BA_DING)

music.play(music.FUNK)

music.play(music.PUNCHLINE)

music.play(music.POWER_UP)

music.play(music.POWER_DOWN)

# data[].attributes.body
while True:

# data[].attributes.body
from microbit import *

while True: if button_b.is_pressed(): display.show(Image('00600:06960:69996:69996:06960')) sleep(1000) if pin0.read_analog()<600: display.show(Image.SAD) sleep(1000) else: display.show(Image.HAPPY) sleep(1000) display.scroll(str(pin0.read_analog()))

# data[].attributes.body
pin0.read_analog()

# data[].attributes.body
from microbit import *

display.show(Image.HAPPY)

# data[].attributes.body
display.show(Image.HAPPY)

# data[].attributes.body
from microbit import *
display.show(Image.GIRAFFE)
sleep(2000)
display.show(Image.DUCK)

# data[].attributes.body
from microbit import *
display.show(Image.NO)
sleep(2000)
display.show(Image.STICKFIGURE)

# data[].attributes.body
from microbit import *

display.show(Image('90909:09990:99999:09990:90909'))

Try different brightnesses — remember 9 is bright, 5 is medium brightness, 1 is very dim, and 0 is off.

# data[].attributes.body
from microbit import *
while True:
if button_a.is_pressed():
display.show(Image.DUCK)

# data[].attributes.body
while True:
display.scroll(str(pin0.read_analog()))

# data[].attributes.body
while True:
display.show(Image.BUTTERFLY)
sleep(500)
display.show(Image.HAPPY)
sleep(500)

# data[].attributes.body
from microbit import *

display.scroll(str(temperature()))

# data[].attributes.body
from microbit import *
display.scroll('Hello, micro:bit!')

# data[].attributes.body
Image.HEART

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

# data[].attributes.body
from microbit import *

while True:
if button_a.is_pressed():
display.show(Image.DUCK)
elif button_b.is_pressed():
display.show(Image.GIRAFFE)
else:
display.clear()

# data[].attributes.body
from microbit import *
import music

music.play('C')
music.play('D')
music.play('E')
music.play('F')
music.play('G')
music.play('A')
music.play('B')