Plan it, then type it
A good plan in pseudocode is easy to turn into Python. The IF line becomes an if line with a colon at the end instead of THEN. The lines inside are indented, and the indent replaces ENDIF, so Python has no ENDIF. Python needs int() around input when a number is wanted, because input always gives text. In pseudocode we compare with a single =, but in Python a single = stores a value, so the comparison operator is a double ==. The other operators are the same as in pseudocode: !=, <, >, <= and >=. A program that breaks these rules will not run. In Python the lines that are indented the same amount belong together.
| Pseudocode | Python |
|---|---|
| INPUT height | height = int(input("Height in cm? ")) |
| IF height >= 120 THEN | if height >= 120: |
| OUTPUT "You can ride" | print("You can ride") |
| ENDIF | (nothing: the indent shows the end) |
1height = int(input("Height in cm? ")) 2if height >= 120: 3 print("You can ride")
You can ride
1a = 7 2b = 12 3print(a == b) 4print(a != b) 5print(a < b) 6print(a >= b)
False True True False
| Meaning | Pseudocode | Python |
|---|---|---|
| equal to | = | == |
| not equal to | != | != |
| less than | < | < |
| greater than or equal to | >= | >= |
Translate the plan (no computer). Write this pseudocode on paper: INPUT cards / IF cards >= 10 THEN / OUTPUT "Enough cards" / ENDIF. Underline the parts that must change to become Python: THEN, the single =, INPUT, OUTPUT and ENDIF. Write the Python version below it. Check each line with a partner using a coloured pen for the parts that changed. Try it with two other plans of your own, for example about stamps or marbles. If you have a computer or tablet, type one program into IDLE and run it with a number above and below the limit.
1INPUT laps 2IF laps >= 5 THEN 3 OUTPUT "Medal" 4ENDIF
1score = 10 2if score > 10: 3 print("Higher") 4if score == 10: 5 print("Exactly ten") 6print("Done")
1x = 5 2if x = 5: 3 print("Five")