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.
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.
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.
Job
Python
MicroPython
get the tools
import time
from microbit import *
show text
print("Go")
display.scroll("Go")
wait 2 seconds
time.sleep(2)
sleep(2000)
calculate
left = coins - spent
left = coins - spent
show a number
print(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.
The output is 9, because 15 − 6 = 9. With spent = 11 it is 4. Python: coins = 15, spent = 6, left = coins - spent, then print(left). MicroPython: from microbit import *, then the same three lines, then display.scroll(left).
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.
Test
spent =
Expected outcome
Actual outcome
Pass/fail
1
2
?
2
6
?
3
15
?
4
0
?
Expected outcomes: test 1: 13 (15 − 2); test 2: 9 (15 − 6); test 3: 0 (15 − 15); test 4: 15 (15 − 0). Running the program should give the same numbers, so every test passes.
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
display.scroll belongs to the micro:bit (MicroPython) language. Python does not know the name display, so it stops with a NameError. In Python the line should be print("Go").
4
Copy the table and fill in the empty cells. Remember that 1 second is 1000 milliseconds.