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

Debugging and light grids

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 questionA 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 1Frame 2Frame 3Frame 4Frame 5Frame 6
greenorangepurplegreenpurpleorange
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.
StartDisplay an arrow pointing upwait 0.3 secondDisplay an arrow pointing rightwait 0.3 secondDisplay an arrow pointing downwait 0.3 secondDisplay an arrow pointing leftwait 0.3 secondEnd
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.
Python
1from microbit import *
2display.show(Image.ARROW_N)
3sleep(300)
4display.show(Image.ARROW_S)
5sleep(300)
6display.show(Image.ARROW_E)
7sleep(300)
8display.show(Image.ARROW_W)
9sleep(300)
Output
the arrows show up, down, right, 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 codeChecklist question
1display.show(Image.DIAMOND?
2if score > 3?
3display.scroll(Welcome)?
4display.show(image.SQUARE)?
5sleap(200)?
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.
Python
1from microbit import *
2display.show(Image.ARROW_N)
3sleep(1000)
4display.show(Image.HEART)
5sleep(1000)
6display.show(Image.ARROW_N)
7sleep(100)
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 1Frame 2Frame 3Frame 4Frame 5Frame 6Frame 7Frame 8
uprightdownleftuprightleftleft
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?