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

Prototypes and patterns

Week 26 · Unit 7.6: Sequencing and pattern recognition · 3 lessons
3

Spot the pattern, reuse it

Computing 7: Unit 7.6, pp. 182–224
GoalUse pattern recognition to compare block and text code, to copy and edit a program, and to continue a sequence.
New words
pattern recognition · spotting what repeats or stays the same, and using it to help solve a problemblock-based programming · making programs by joining blocks on the screen, as in Scratchtext-based programming · making programs by typing lines of code, as in Pythonindentation · moving a line in from the left to show that it belongs to the line above
Explanation

Pattern recognition means spotting what repeats or stays the same and using it to help solve a problem. You can use it to continue a pattern, such as a row of lights. You can also use it to compare two programs. A block program and a text program share the same key words, such as button, A, show and heart. In the text program, the first line asks about the button, and the indented line under it is what happens inside it. When you see this pattern, you can copy a program and edit only the parts that change, such as the button name and the picture name. Capital letters are part of the pattern too: HEART and ARROW_W are written in capitals. Flowcharts can also be put side by side to find what stays the same.

Blocksshow iconhearton buttonApressed
Worked examples
Python
1from microbit import *
2while True:
3    if button_a.is_pressed():
4        display.show(Image.HEART)
Output
a heart appears when button A is pressed (and stays, because nothing clears it)
Here is the same program in text. The block “on button A pressed” matches the line if button_a.is_pressed():. The block “show icon heart” matches the indented line display.show(Image.HEART). The key words button, a, show and heart appear in both.
Python
1from microbit import *
2while True:
3    if button_a.is_pressed():
4        display.show(Image.ARROW_W)
5    if button_b.is_pressed():
6        display.show(Image.ARROW_E)
Output
pressing A shows an arrow pointing left; pressing B shows an arrow pointing right (each stays until another picture replaces it)
Copy and edit. Amir makes a bicycle indicator. Button A shows a left arrow. To add button B he copies the A lines and changes only two things: button_a becomes button_b, and ARROW_W becomes ARROW_E. Everything else is the same pattern.
Part of the flowchartButton A flowchartButton B flowchart
Shape of the questiondecision diamonddecision diamond (the same)
The questionIs button A pressed?Is button B pressed?
The outputDisplay a left arrowDisplay a right arrow
Order of the stepsquestion, then outputquestion, then output (the same)
The flowcharts for the two buttons also share a pattern. Look at what stays the same and what changes.
Class activity

Pattern hunt (no computer). Write the bicycle indicator program from the lesson on a card. Colour the parts of each if block that stay the same in blue, and the parts that change in orange. Then invent a third program: button A shows Image.SMILE and button B shows Image.TARGET. Ask someone to copy and edit the program, using only your colours as hints. Check the capital letters.

Practice
1
This flowchart shows a pattern. Continue it so that the same two images are shown again, in the same order. Draw the extra boxes and write the four extra lines of MicroPython.
StartDisplay a housewait 2 secondsDisplay a duckwait 2 secondsEnd
2
Hana copied the A lines to make the B lines. When A is held down, the program shows an arrow pointing down, not up. When B is held down, nothing happens. Find the mistake, and say what to change.
Python
1from microbit import *
2while True:
3    if button_a.is_pressed():
4        display.show(Image.ARROW_N)
5    if button_a.is_pressed():
6        display.show(Image.ARROW_S)
3
These blocks make a program. Write the MicroPython lines that do the same, using the pattern from the lesson.
Blocksshow iconyeson buttonBpressed
4
Explain how pattern recognition helps a programmer who copies and edits a program instead of writing a new one from nothing.