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

Ladders and mixed-up lines

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

The elif ladder

Computing 8: Topic 1.4, pp. 70–105
GoalWrite a Python if ... elif ... else chain and explain why only the first true branch runs.
New words
elif · short for else if: a further condition that is tested only when every condition above it was falsechain · an if with one or more elif parts, and often a final elselogic error · a mistake that does not stop the program, but makes it give the wrong answer
Explanation

Python has no ELSEIF. It has elif, which is one word, followed by a condition and a colon. A chain starts with if, has any number of elif parts and may end with else. Python tests the conditions from the top. The first true condition runs its indented lines, and then Python skips the rest of the chain. This means the order of the conditions matters, just as it did in pseudocode. If the order is wrong the program still runs, but it gives wrong answers, which is a logic error. In Python, print with a comma between items puts a space between them, so print("Fare:", fare) shows Fare: 4. In our pseudocode the comma added no space.

PseudocodePython
IF km <= 3 THENif km <= 3:
ELSEIF km <= 10 THENelif km <= 10:
ELSEelse:
ENDIF(nothing)
Worked examples
km typedFare printed
2Fare: 4
3Fare: 4
4Fare: 9
10Fare: 9
26Fare: 30
A taxi fare chain. The ranges are 3 km or less, up to 10 km, up to 25 km, and anything longer. Each distance follows the first true condition.
Python
1km = int(input("Distance in km? "))
2if km <= 3:
3    fare = 4
4elif km <= 10:
5    fare = 9
6elif km <= 25:
7    fare = 18
8else:
9    fare = 30
10print("Fare:", fare)
Output
Fare: 9
The taxi program in Python. With 4 typed, km <= 3 is false and km <= 10 is true, so fare becomes 9. The remaining elif and else are skipped.
Python
1km = int(input("Distance in km? "))
2if km <= 25:
3    fare = 18
4elif km <= 10:
5    fare = 9
6elif km <= 3:
7    fare = 4
8else:
9    fare = 30
10print("Fare:", fare)
Output
Fare: 18
The same conditions in the wrong order give a logic error. The program runs, but every distance up to 25 km gets the 18 fare, because km <= 25 is true first. With 2 km typed it prints Fare: 18.
Class activity

Ladder of cards (no computer). Draw four rungs on a large sheet. Write on each rung one condition from the taxi program, with the last rung as else. Use a counter to climb: it starts on the first rung and moves down only when the rung’s condition is false. A partner calls a distance (2, 3, 4, 10, 25 and 26). Write the fare where the counter stops. Then swap the first two rungs and call 2 again. What changed? If you have a computer or tablet, ask an adult to run both versions of the program.

Practice
1
Follow the program with 5, 2 and 1 stars. What is printed each time?
Python
1stars = int(input("Stars? "))
2if stars >= 4:
3    print("Great")
4elif stars >= 2:
5    print("Okay")
6else:
7    print("Poor")
2
Rewrite this pseudocode in Python.
Pseudocode
1INPUT sold
2IF sold >= 100 THEN
3    OUTPUT "Sold out"
4ELSEIF sold >= 50 THEN
5    OUTPUT "Selling well"
6ELSE
7    OUTPUT "Slow"
8ENDIF
3
This program should print Big for 20 or more and Medium for 10 or more. What does it print for 25, and what type of error is this? How do you fix it?
Python
1n = int(input("Number? "))
2if n >= 10:
3    print("Medium")
4elif n >= 20:
5    print("Big")
6else:
7    print("Small")
4
A chain has an if, three elif parts and an else. The first condition is true. How many conditions does Python test? How many would be tested if the four parts were separate if statements?