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 line
Example
What it does
first check
button_a.is_pressed()
true while A is held down
joining word
and
both checks must be true
second check
score >= 5
true when score is 5 or more
end of the line
a colon :
the indented lines below it run only if the whole condition is true
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.
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.
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.
hour = 8: a target, because 8 >= 6 and 8 < 10 are both true. hour = 5: nothing, because 5 >= 6 is false. hour = 6: a target, because 6 >= 6 is true. hour = 9: a target. hour = 10: nothing, because 10 < 10 is false. The light is on from 6 up to, but not including, 10.
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?
She used and, but she needs or. With and, both buttons must be pressed. Pressing both gave true with and and with or, so her one test could not tell them apart. A good test plan uses four cases: only A, only B, both, and neither. Only A and only B would have failed. Fix: change and to or.
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.
(a) if button_a.is_pressed() or button_b.is_pressed(): (b) if button_a.is_pressed() and not button_b.is_pressed(): (c) if age >= 8 and age <= 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.
It should use AND. With AND both buttons must be pressed, so both hands are on the buttons and away from the moving parts. With OR one button would be enough, and the other hand could still be inside the machine.