Week 28 · Unit 7.6: Sequencing and pattern recognition · 3 lessons
1
Bugs in the sequence
Computing 7: Unit 7.6, pp. 182–224
GoalFind sequence errors and typing errors by comparing a flowchart with code and by using a checklist.
New words
debugging · finding the errors in a program and fixing themsequence · the order of the instructions; if the order is wrong, the output is wrongsyntax error · a mistake in how the code is written, so the program will not startruntime error · a mistake that stops the program while it is running, such as using a name the program does not know
Explanation
The order of the instructions is called the sequence, and it decides what a program does. If two lines are in the wrong order, the program runs but gives the wrong output. Pattern recognition helps to find such a mistake. Look for the pattern, such as a picture followed by a wait, and then look for the place where the pattern breaks. You can also put the flowchart next to the code and check them line by line. If the sequence is right, look for typing mistakes. A missing bracket or colon is a syntax error, and a misspelt name is a runtime error. In both cases the micro:bit scrolls a message across the lights that tells you the line. Use the checklist to find the mistake.
Checklist question
A mistake it catches
Does the program start with from microbit import *?
the line is missing, so display is not known
Are all brackets opened and closed?
display.show(Image.HEART
Is there a colon at the end of the if line?
if button_a.is_pressed()
Are the capital letters correct?
Image.heart instead of Image.HEART
Is the string inside quotation marks?
display.scroll(Hello)
Are the code words spelled correctly?
sleeep(500)
Worked examples
Frame 1
Frame 2
Frame 3
Frame 4
Frame 5
Frame 6
green
orange
purple
green
purple
orange
Spot the pattern. A light panel should repeat green, orange, purple. The pattern shows that frames 5 and 6 are the wrong way round: frame 4 is green, so orange should come next, and then purple.
This flowchart makes a compass spinner. The pattern is a picture followed by a wait, four times. The arrows turn clockwise: up, right, down, left.
This is the program for the compass spinner, but the arrows do not turn clockwise. Line up the code with the flowchart. The second picture should be right (ARROW_E), but line 4 shows down (ARROW_S). The third picture should be down, but line 6 shows right. Swapping lines 4 and 6 fixes the sequence. There is no typing mistake, so the program runs: this is a logic error.
Class activity
Bug hunt swap (no computer). Write a six-line micro:bit program on paper that shows three pictures with waits, and draw its flowchart. Plant two bugs in a copy of the program: one line in the wrong order and one typing mistake from the checklist. Swap with a partner. They must find both bugs by lining the code up with the flowchart and by going through the checklist. Mark each bug with a coloured pen and write the correct line.
Practice
1
Copy the table. Which checklist question catches each mistake? Write brackets, colon, capitals, quotation marks or spelling.
Line of code
Checklist question
1
display.show(Image.DIAMOND
?
2
if score > 3
?
3
display.scroll(Welcome)
?
4
display.show(image.SQUARE)
?
5
sleap(200)
?
1 brackets (the ) is missing). 2 colon (the if line needs a colon at the end). 3 quotation marks (Welcome must be a string in quotation marks). 4 capitals (it must be Image.SQUARE). 5 spelling (the word is sleep).
2
The flowchart says: show an up arrow, wait 1 second, show a heart, wait 1 second, show a down arrow, wait 1 second. Line the program up with the flowchart, find the two differences and write the correct program.
Line 6 shows the up arrow again, but the third picture should be the down arrow, so line 6 should be display.show(Image.ARROW_S). Line 7 is sleep(100), but the last wait should be 1 second, so it should be sleep(1000).
3
The pattern of arrows should be up, right, down, left, up, right, down, left. One frame is wrong. Which frame is it, and what should it be?
Frame 1
Frame 2
Frame 3
Frame 4
Frame 5
Frame 6
Frame 7
Frame 8
up
right
down
left
up
right
left
left
Frame 7 is wrong. The pattern repeats up, right, down, left, so frame 7 should be down, not left. Frame 8 (left) is correct.
4
The micro:bit scrolls a message across the lights when your program has a problem. What does the message tell you, and what should you do next?
The message names the kind of mistake and the line number. This tells you the problem is a typing mistake, not the sequence. Open that line, check it against the list of brackets, colon, capitals, quotation marks and spelling, fix it and run the program again.