Week 8 · Unit 7.2: Decomposing problems · 3 lessons
3
Does the code match the plan?
Computing 7: Unit 7.2, pp. 44–81
GoalTrace a sensor flowchart, check the program against it and add a sound output.
New words
import · a line that loads extra commands, such as import musictrace · to follow a flowchart or program step by step using a chosen valueoutput · what a program shows, says or does, such as a picture or a tuneruntime error · a mistake that stops a program while it is running
Explanation
To make a sound, a program must first load the music library with the line import music. Then music.play(music.POWER_DOWN) plays a short tune. A program can have more than one output, such as a picture and a tune. To check a program, trace its flowchart with some sensor values. Then compare the program with the flowchart line by line: the same sensor, the same operator and the same threshold. Common bugs are a wrong operator, a wrong sensor, a missing colon or a missing import. A missing import is a runtime error, because the program starts but stops when it reaches the music line. In the program the whole flowchart sits inside while True, so it is followed again and again.
when the temperature is below 12: a cross and a tune
This is the program for the flowchart. A plant-room frost warning shows a cross and plays a tune when it is cold. Notice the import line at the top.
Temperature
Question: temperature < 12?
Output
5
yes
cross and tune
12
no
nothing new
20
no
nothing new
0
yes
cross and tune
Tracing means choosing values and following the flowchart. Only a value below 12 takes the yes path.
Bug
Example
Kind
wrong operator
> instead of <
logic error
wrong sensor
display.read_light_level() instead of temperature()
logic error
missing colon
if temperature() < 12
syntax error
missing import
music.play(...) with no import music
runtime error
When a program does not match its flowchart, look for one of these bugs.
Class activity
Trace race (no computer). Your partner writes eight numbers from 0 to 40 in a row. You trace the frost-warning flowchart for each number and write “cross and tune” or “nothing new” under it. Your partner checks your answers. Then change the threshold to 20 and the operator to <=, redraw the flowchart and trace the same numbers. Which answers changed? Why?
Practice
1
What does this program do when the light level is 99, 100 and 101?
The line import music is missing. The program only reaches music.play when the temperature is above 30, and then it stops. This is a runtime error. The fix is to add import music under the first line.
3
The flowchart asks “temperature >= 30?” but the program uses > 30. When do they give different results? Write the correct operator.
They differ only when the temperature is exactly 30: the flowchart takes the yes path, but the program does not. The program should use >=.
4
Write the MicroPython program for this flowchart.
Program (each → is 4 spaces of indentation): 1 from microbit import * | 2 import music | 3 while True: | 4 → if accelerometer.was_gesture("shake"): | 5 → → display.show(Image.HEART) | 6 → → music.play(music.JUMP_UP)