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

Two languages, one plan

Week 27 · Unit 7.6: Sequencing and pattern recognition · 3 lessons
1

Python and micro:bit twins

Computing 7: Unit 7.6, pp. 182–224
GoalFind the matching parts in the same program written in Python and in MicroPython.
New words
string · text inside quotation marks, such as "Go"sequence · the order in which the instructions are run, from top to bottommillisecond · one thousandth of a second: 1000 milliseconds make 1 secondimport · a line that brings in ready-made code so that the program can use it
Explanation

Python and MicroPython are both text-based languages, so they share many patterns. To show text, Python uses print("Go") and MicroPython uses display.scroll("Go"). In both, the text is inside brackets and quotation marks, which tell the computer that it is a string. To wait, Python uses time.sleep(2), with the number in seconds. MicroPython uses sleep(2000), with the number in milliseconds. One second is 1000 milliseconds, so half a second is 500. Both programs run in sequence from top to bottom, so the order of the lines matters. The * sign has two jobs: in from microbit import * it means everything, and in total = stickers * 3 it means multiply.

StartOUTPUT Readywait 2 secondsOUTPUT Steadywait 2 secondsOUTPUT GoEnd
Worked examples
Python
1import time
2print("Ready")
3time.sleep(2)
4print("Steady")
5time.sleep(2)
6print("Go")
Output
Ready
Steady
Go
This is the flowchart written in Python. The first line, import time, brings in the code that lets the program wait. Each box of the flowchart becomes one line, in the same order.
Python
1from microbit import *
2display.scroll("Ready")
3sleep(2000)
4display.scroll("Steady")
5sleep(2000)
6display.scroll("Go")
Output
Ready, Steady, Go scroll across the lights
This is the same flowchart written in MicroPython for the micro:bit. The shape is the same: output, wait, output, wait, output. Only the words change, and the waits are in milliseconds.
JobPythonMicroPython
get the toolsimport timefrom microbit import *
show textprint("Go")display.scroll("Go")
wait 2 secondstime.sleep(2)sleep(2000)
calculateleft = coins - spentleft = coins - spent
show a numberprint(left)display.scroll(left)
Matching the two languages job by job shows the patterns.
Class activity

Twin cards (no computer). On small cards write each box of the Ready, Steady, Go flowchart. On a second set write the Python lines, and on a third set the MicroPython lines. Shuffle all the cards and match them into groups of three. Time yourself, then change the waits to 1 second and make the new cards. If you have a computer or tablet, type the Python program into IDLE with an adult and watch the pauses.

Practice
1
Look at the flowchart. What is the output? What would it be if spent = 11? Then write the program in Python and in MicroPython.
Startcoins = 15spent = 6left = coins - spentOUTPUT leftEnd
2
Copy this test plan and work out the expected outcomes by hand, before you run any program. The program is the one in the flowchart above, with a new value of spent each time.
Testspent =Expected outcomeActual outcomePass/fail
12?
26?
315?
40?
3
Hugo wrote this Python program, but it stops with the message shown. What is wrong, and how do you fix it?
Python
1import time
2display.scroll("Go")
Output
NameError: name 'display' is not defined
4
Copy the table and fill in the empty cells. Remember that 1 second is 1000 milliseconds.
PythonMicroPython
time.sleep(3)sleep(?)
time.sleep(0.5)sleep(?)
time.sleep(0.25)sleep(?)
time.sleep(?)sleep(4000)
time.sleep(?)sleep(1500)