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
3

One plan, three forms

Computing 8: Topic 1.4, pp. 70–105
GoalShow the same algorithm as a flowchart, pseudocode and Python, and rebuild a program from scrambled lines.
New words
equivalent · doing exactly the same job even though it is written differentlyscrambled · mixed up, in the wrong orderdry run · following a program by hand with chosen inputs, without a computer
Explanation

A good designer can move an algorithm between a flowchart, pseudocode and Python without changing what it does. A diamond in a flowchart becomes IF in pseudocode and if in Python. A diamond on a no arrow becomes ELSEIF and then elif. The last no arrow becomes ELSE and then else. The indentation in Python replaces ENDIF. The three versions must give the same answer for every input, so a dry run with the same test values is a good check. When lines of a program are mixed up, put the first line first, find the if, then the elif parts in order and the else last. Then check the indentation.

StartINPUT levellevel < 20?yesnoOUTPUT "Refill now"level < 60?yesnoOUTPUT "Half full"OUTPUT "Plenty"End
Worked examples
Pseudocode
1INPUT level
2IF level < 20 THEN
3    OUTPUT "Refill now"
4ELSEIF level < 60 THEN
5    OUTPUT "Half full"
6ELSE
7    OUTPUT "Plenty"
8ENDIF
The water tank flowchart as pseudocode. The values 10, 20, 59 and 60 test the edges of the ranges.
Python
1level = int(input("Level percent? "))
2if level < 20:
3    print("Refill now")
4elif level < 60:
5    print("Half full")
6else:
7    print("Plenty")
Output
Half full
And as a Python program. With 20 typed the program prints Half full. The result for every input is the same as for the pseudocode: 10 prints Refill now, 20 and 59 print Half full, and 60 prints Plenty.
level typedFlowchart pathPseudocode outputPython output
10yes at first diamondRefill nowRefill now
20no, then yesHalf fullHalf full
59no, then yesHalf fullHalf full
60no, then noPlentyPlenty
One dry run in all three forms. The edge values show that the three versions agree.
Class activity

Three forms game (no computer). Work in a team of three. Choose a situation with three outcomes, such as the amount of light in a room or the number of players for a team game. Person A draws the flowchart, Person B writes the pseudocode and Person C writes the Python. Do not look at each other’s work until all three are finished. Then each person dry-runs another person’s version with the same three test values and compares answers. Fix any version that disagrees. Cut the Python program into strips, one line per strip, shuffle them, and let another team put them back in order. If you have a computer or tablet, type your Python program and run it.

Practice
1
These lines of a Python program are mixed up. Put them in the right order and add the indentation.
LetterLine
Aprint("Nearly full")
Bpassengers = int(input("Passengers? "))
Celse:
Dif passengers > 40:
Eprint("Seats free")
Felif passengers > 30:
Gprint("Bus is full")
2
Write the flowchart as a Python program. Use if, elif and else.
StartINPUT snowsnow > 30?yesnoOUTPUT "Sledge"snow > 10?yesnoOUTPUT "Boots"OUTPUT "Shoes"End
3
Find the three mistakes in this program and write it correctly.
Python
1mood = input("Mood? ")
2if mood = "happy":
3    print("Smile")
4else if mood == "tired":
5    print("Rest")
6else
7    print("Carry on")
4
Why is the Python version shorter than the pseudocode version, and is it exactly the same algorithm?