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

Python makes the choice

Week 6 · 1.4 Conditional statements in text-based programming · 3 lessons
1

Plan it, then type it

Computing 8: Topic 1.4, pp. 70–105
GoalTurn a pseudocode IF statement into a Python if statement and use the Python comparison operators correctly.
New words
if statement · a Python instruction that runs its indented lines only when the condition is truecolon · the symbol : at the end of an if line, which Python needsindent · spaces at the start of a line that show it belongs inside the if== · the Python operator that asks whether two values are equal
Explanation

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.

PseudocodePython
INPUT heightheight = int(input("Height in cm? "))
IF height >= 120 THENif height >= 120:
OUTPUT "You can ride" print("You can ride")
ENDIF(nothing: the indent shows the end)
Worked examples
Python
1height = int(input("Height in cm? "))
2if height >= 120:
3    print("You can ride")
Output
You can ride
The fairground plan, written in Python. With 130 typed the condition is true and the message is shown. With 110 typed the condition is false and nothing is printed.
Python
1a = 7
2b = 12
3print(a == b)
4print(a != b)
5print(a < b)
6print(a >= b)
Output
False
True
True
False
Python prints True or False for a comparison. Here a is 7 and b is 12. Notice the double equals sign for a comparison, and the single equals sign for storing.
MeaningPseudocodePython
equal to===
not equal to!=!=
less than<<
greater than or equal to>=>=
Pseudocode compared with Python for the comparison operators. Only the equals sign is different.
Class activity

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.

Practice
1
Rewrite this pseudocode as a Python program.
Pseudocode
1INPUT laps
2IF laps >= 5 THEN
3    OUTPUT "Medal"
4ENDIF
2
Predict what this program prints. There is no input.
Python
1score = 10
2if score > 10:
3    print("Higher")
4if score == 10:
5    print("Exactly ten")
6print("Done")
3
Find the mistake in the first line of this program and explain it. What should it be?
Python
1x = 5
2if x = 5:
3    print("Five")
4
Write the Python condition for each. (a) height is at most 140. (b) name is not "Ali". (c) score is exactly 100.