AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Typing for the micro:bit

Week 7 · Unit 7.2: Decomposing problems · 3 lessons
1

First lines of MicroPython

Computing 7: Unit 7.2, pp. 44–81
GoalWrite and read short MicroPython programs that show text and pictures, and obey the syntax rules.
New words
MicroPython · the version of Python made for the micro:bitsyntax · the rules that a program must follow so that the computer can understand itlibrary · a collection of ready-made commands that a program can usestring · text inside quotation marks, made of letters, numbers or other characters
Explanation

Python for the micro:bit is called MicroPython. Every micro:bit program starts with the line from microbit import *, which loads the library of micro:bit commands; the star means “everything in it”. The command display.scroll("Hi") moves text across the lights. Text must be inside quotation marks, which makes it a string. The command display.show(Image.HEART) shows a picture, and picture names are written in capital letters. MicroPython syntax is strict about capital letters, brackets, dots and quotation marks, so one wrong letter, such as Image.heart, stops the line from working. The command sleep(1000) pauses the program for 1000 milliseconds, which is 1 second. To try a finished program on the real board, an adult connects it with a USB cable and flashes the program onto it.

Python
1from microbit import *
2display.scroll("Hi")
3display.show(Image.HEART)
Output
scrolls Hi, then shows a heart
Worked examples
Python
1from microbit import *
2display.show(Image.SQUARE)
3sleep(1000)
4display.show(Image.SQUARE_SMALL)
Output
big square for 1 second, then a small square
This program shows a big square, waits one second, and then shows a small square. The pause is needed: without it the second picture would replace the first so fast that you could not see the first one.
LineDoes it work?Why
display.scroll("Hi")yestext is in double quotes
display.scroll('Hi')yestext is in single quotes
display.scroll(Hi)noHi is not in quotes, so it is not text
display.scroll("Hi')nothe quotes do not match
Quotation marks tell the computer that something is text. Without them, Python thinks the word is the name of something else. Single or double quotation marks both work, but they must match.
LineWorks?
display.show(Image.YES)yes
display.show(Image.yes)no – image names are capitals
Display.show(Image.YES)no – display starts with a small d
display.shwo(Image.YES)no – misspelt command
MicroPython is case-sensitive. A capital letter and a small letter are different characters, so only the exact spelling works.
Class activity

Syntax detective (no computer). On paper write six lines of MicroPython: three that are correct and three that each have one mistake, such as a missing quotation mark or a small letter in a picture name. Swap with a friend. They mark the mistakes in red and say which rule was broken. Then write a program that scrolls your first name and then shows a picture of your choice from this list: HEART, YES, NO, SQUARE, HOUSE. If you have a computer or tablet, an adult can help you type it into a micro:bit editor and run it.

Practice
1
Predict what this program does and how long the pause is.
Python
1from microbit import *
2display.scroll("Go")
3sleep(2000)
4display.show(Image.YES)
2
Each of lines 2, 3 and 4 has one mistake. Find them and write the correct lines.
Python
1from microbit import *
2display.scrol("Hi")
3display.show(Image.Heart)
4display.scroll(Bye)
3
Complete the program so that it shows a tick, waits half a second and then shows a cross. (Hint: half a second is 500 milliseconds.)
Python
1from microbit import *
2display.show(Image.YES)
3___(___)
4display.show(Image.___)
4
Write a program that scrolls the word Hello and then shows a small heart (HEART_SMALL).