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
3

Hunt the bug with a flowchart

Computing 7: Unit 7.2, pp. 44–81
GoalFind and fix syntax errors and logic errors by comparing a program with its flowchart.
New words
bug · a mistake in a programdebugging · finding and fixing the mistakes in a programsyntax error · a mistake in the rules of the language, such as a misspelt command or a missing quotation marklogic error · a mistake in the plan: the program runs but does the wrong thing
Explanation

Debugging means finding and fixing bugs. In a text language it is easy to make a mistake. The best method is to put the flowchart next to the program and check the program line by line against the steps. A syntax error breaks the rules of the language, such as scrol, a missing quotation mark or a capital letter that should be a small one. The program will not work properly. A logic error follows the rules but does the wrong thing, such as using button A when the plan says button B, or putting two steps in the wrong order. Many editors change the colour of text in quotation marks, which is a quick way to see that a string is closed. Work in a pair: one reads the plan aloud, the other checks the program.

Kind of errorExampleWhat happens
syntax errordisplay.scrol("Hi")a line breaks the rules, so it cannot work
logic errorbutton_a when the plan says button Bthe program runs but does the wrong thing
Worked examples
StartIs B pressed?yesnoshow a tickwait 2 secondsscroll the word ByeEnd
This is the plan. When button B is pressed, the board shows a tick, waits two seconds and then scrolls the word Bye.
Python
1from microbit import *
2while True:
3    if button_b.is_pressed():
4        display.show(Image.NO)
5        sleep(200)
6        display.scroll("Bye")
This program is meant to follow the plan, but it has bugs. Check each line against the plan. Line 4 shows a cross, but the plan says a tick. Line 5 waits 200 milliseconds, which is only 0.2 seconds, but the plan says 2 seconds.
Python
1from microbit import *
2while True:
3    if button_b.is_pressed():
4        display.show(Image.YES)
5        sleep(2000)
6        display.scroll("Bye")
Output
when B is pressed: a tick for 2 seconds, then Bye scrolls
Here is the corrected program. Both bugs were logic errors, because the program ran but did not follow the plan. The right time is 2 seconds, which is sleep(2000).
Class activity

Bug swap (no computer). Choose a small job, such as “when A is pressed, scroll a greeting and show a heart”, and draw its flowchart. Write the program on paper. Then copy it out again and secretly add two bugs, one syntax error and one logic error. Swap with a partner. They must find both bugs by comparing your program with your flowchart, and say which kind each bug is. If you have a computer or tablet, an adult can help you check whether the corrected program runs.

Practice
1
The plan says: when A is pressed, scroll Hi and show a heart. Find the two syntax errors and write the correct lines.
Python
1from microbit import *
2while True:
3    if button_a.is_pressed():
4        display.scroll(Hi)
5        display.show(Image.heart)
2
Say whether each bug is a syntax error or a logic error. (a) display.shwo(Image.NO) (b) the plan says button B but the code uses button_a (c) a closing quotation mark is missing (d) the plan shows a heart but the code shows a square.
BugSyntax or logic?
display.shwo(Image.NO)?
plan says B, code uses button_a?
closing quotation mark missing?
plan: heart, code: square?
3
The plan says: when B is pressed, show a cross for one second and then show a tick. Find the bug in this program and correct it.
Python
1from microbit import *
2while True:
3    if button_b.is_pressed():
4        display.show(Image.YES)
5        sleep(1000)
6        display.show(Image.NO)
4
Why does numbering the lines of the program and the steps of the flowchart help when you debug?