An if asks a question that is either True or False, and runs one path or the other.
One sign carries most of this block. One equals puts a value in. Two equals asks a question. Mark = 70 stores 70. Mark == 70 asks whether Mark is 70, and gives True or False.
When there are several tests in a row, Python checks them top to bottom and stops at the first one that is True. A cinema sells three tickets: Child under 12, Adult under 60, and Senior for everyone else. Pick an age and step through it.
Try 12. It is not under 12, so the first test is False and the Adult test catches it.
Comparing, and the signs that change from pseudocode
| Pseudocode | Python | Means |
|---|---|---|
| = | == | is equal to |
| <> | != | is not equal to |
| < > <= >= | < > <= >= | the same in both |
| AND OR NOT | and or not | lower case |
| MOD | % | remainder |
if, elif, else, and the indentation is the ENDIF
Every if line ends with a colon, and the lines it controls are indented underneath. There is no ENDIF: the indentation shows where the branch stops.
elif means "else, if". Use as many as you need. else has no test: it catches everything the tests above did not.
CASE OF in pseudocode becomes an elif chain in Python.
The first True test wins, so put the narrowest first
Both programs below use the same three tests. Only the order changed.
5 is under 60, so the wrong order stops at the first test and never asks about 12. A child pays the adult price. Grade bands work the same way: test Mark >= 80 before Mark >= 70.
Comparing text: capitals, and numbers that are still text
"yes" == "Yes" is False. Capital letters count. Convert first, then compare with one spelling:
Text compares letter by letter, not by value. As text, "9" > "10" is True, because the character 9 comes after the character 1. Forget int() on two ages and your program names the wrong person as older, with no error at all.
To swap two boxes you need a third
Putting three numbers in order means swapping two of them when they are the wrong way round. A = B then B = A does not work: the first line wipes out the old A. Keep it somewhere first:
You will use exactly this in bubble sort, much later in the course.
Six ways this block goes wrong
TRAPUsing one equals in a test.
if Mark = 70:stops with a SyntaxError. A test needs==.Tests in the wrong order.
The wider test catches everything first. Narrowest test at the top.
Comparing numbers that are still text.
"9" > "10"is True. Convert withint()as you read.Forgetting the colon.
Every
if,elifandelseline ends with:.Capitals in text answers.
Use
.upper()on what was typed, then compare with capitals.Writing
ANDor<>.Python wants
andand!=.
What you do and don’t need
You do need
if,elifandelse, indented correctly- all six comparisons, plus the words
and,orandnot %for remainders,//for whole division.upper()before comparing text- swapping two values with a spare variable
Not yet, or not at all
- loops: block C
- Python's
matchstatement. Anelifchain does the same job and is what this course uses infor checking several values at once: this course writes each test withorso you see every one
Test the edges, not just one easy value
A grade program that works for 72 can still be wrong at 70. Run it with a value from every band and with the exact boundary values: 69, 70, 79, 80. The mistakes in decisions hide at the boundaries, usually a > that should have been >=.
Remember these three
- One equals stores. Two equals asks.
- Python checks top to bottom and stops at the first True test. Narrowest test first.
- Convert numbers before comparing them. As text,
"9" > "10".
Steps 8 to 16
Step 14, Each digit as a word, uses the operators from Block A. Read it there.
See all 9 stepsBlock B · DecisionsProgress is saved on this device.