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

Two languages, one plan

Week 27 · Unit 7.6: Sequencing and pattern recognition · 3 lessons
2

AND, OR and NOT

Computing 7: Unit 7.6, pp. 182–224
GoalWrite and trace conditions that join two checks with and, or and not in a MicroPython program.
New words
condition · a check that is either true or false, such as button_a.is_pressed() or score >= 5Boolean operator · a word such as and, or or not that joins or reverses conditionsselection · a choice in a program: a condition is tested and the program follows the if path or the else path
Explanation

A condition is a check that is either true or false. On the micro:bit, button_a.is_pressed() is true while A is held down and false otherwise. A condition can also compare a number, such as score >= 5. Two conditions can be joined. With and, both must be true. With or, at least one must be true. With not, the answer is turned round. In MicroPython these words are written in lower case, and each side of and or or must be a complete check, such as button_a.is_pressed() and score >= 5. To trace a program, work out true or false for each check first, then use the joining word, then follow the if path or the else path.

Part of the lineExampleWhat it does
first checkbutton_a.is_pressed()true while A is held down
joining wordandboth checks must be true
second checkscore >= 5true when score is 5 or more
end of the linea colon :the indented lines below it run only if the whole condition is true
Worked examples
Python
1from microbit import *
2score = 7
3while True:
4    if button_a.is_pressed() and score >= 5:
5        display.show(Image.HEART)
6    else:
7        display.clear()
Output
a heart while A is held down; the lights go off when A is let go
AND. A star badge: the heart shows only while A is held down and the score is at least 5. With score = 7 the second check is true, so everything depends on button A. With score = 4 the heart would never show, even when A is pressed.
Python
1from microbit import *
2hour = 22
3while True:
4    if hour < 7 or hour >= 20:
5        display.show(Image.DIAMOND_SMALL)
6    else:
7        display.clear()
Output
the small diamond stays lit, because hour is 22
OR. A night-light that reads the hour of the day: the small diamond shows when the hour is before 7 or is 20 or later. With hour = 22 the second check is true, so the light is on. With hour = 12 both checks are false, so it is off. The boundaries matter: 20 is on, and 7 is off.
Python
1from microbit import *
2while True:
3    if button_a.is_pressed() and not button_b.is_pressed():
4        display.show(Image.ARROW_E)
5    else:
6        display.show(Image.SQUARE)
Output
right arrow: A only; square: anything else
NOT with AND. A toy train signal: the arrow shows only when A is pressed and B is not pressed, so B works as a stop button. Only A: the arrow. A and B together: the square. Only B: the square. No button: the square.
Class activity

Condition cards (no computer). Make cards for the checks ‘A pressed’, ‘B pressed’, ‘score is at least 5’ and ‘hour is before 7’. Turn each card to true or false. Your partner reads out a joined condition, such as ‘A pressed and score is at least 5’ or ‘not B pressed’, and you say whether the if path or the else path runs. Change one card and see which answers change. Then write three joined conditions of your own for a light in the school hall.

Practice
1
Read the program. What does the display show when hour = 8? Then say what it shows if hour is changed to 5, to 6, to 9 and to 10, and why.
Python
1from microbit import *
2hour = 8
3while True:
4    if hour >= 6 and hour < 10:
5        display.show(Image.TARGET)
6    else:
7        display.clear()
2
Lola wants a heart to show when A is pressed or B is pressed. She wrote the program below and tested it by pressing both buttons together. The heart appeared, so she thought it was right. Later, pressing only A did nothing. What is wrong, and which test data would have found the bug?
Python
1from microbit import *
2while True:
3    if button_a.is_pressed() and button_b.is_pressed():
4        display.show(Image.HEART)
5    else:
6        display.clear()
3
Write the if line (with the colon) for each: (a) the light shows when button A is pressed or button B is pressed; (b) the light shows when A is pressed and B is not pressed; (c) the light shows when the variable age is at least 8 and at most 12.
4
A machine must start only when the operator presses two buttons, one with each hand. Should the program use AND or OR? Explain why this is the safer choice.