GoalBreak a short story into inputs, a process, a condition and outputs, draw the flowchart and trace it with test data.
New words
scenario · a short description of a problem that a program has to solveoutcome · what a program shows or does for given inputs
Explanation
Break a scenario into small parts before you draw. Find the inputs, the process, the condition and the outputs. Then draw the symbols in that order. The condition goes in a diamond, and each possible outcome gets its own branch. When you have the flowchart, you can write Python. In Python, a selection uses if, else and a colon, and the lines under them are indented. You are only asked to read this code for now. Finally, trace the flowchart with test data that goes down every path, including the boundary value.
Worked examples
Part of the story
Becomes
enters the size and how much was drunk
INPUT size, INPUT drunk
works out what is left
process: left = size - drunk
if something is left
condition: left > 0?
Keep drinking / Well done
two OUTPUT boxes
Look at the story: ‘A child enters the size of a water bottle and how much has been drunk. The program works out what is left. If something is left, it says Keep drinking. Otherwise it says Well done.’ We can mark its parts.
Python
1size = int(input("Bottle size in ml? "))
2drunk = int(input("Drunk so far in ml? "))
3left = size - drunk
4if left > 0:
5print("Keep drinking")
6else:
7print("Well done")
Output
Keep drinking
This is the same program in Python. Read the lines from the top. The if line holds the condition, and its colon starts the yes path. The else line starts the no path. The user types 500 and 200.
size
drunk
left
left > 0?
Output
500
200
300
yes
Keep drinking
500
500
0
no
Well done
500
620
−120
no
Well done
Trace the program with test data that visits both branches. The second row is the boundary value.
Class activity
Story strips (no computer): write the scenario ‘Enter how many pages you have read in a book of 100 pages; if there are still pages left, say Keep reading, otherwise say Finished’ as separate strips of paper: one for each input, process, condition and output. Mix them up. A partner puts them into a flowchart order on the table, and draws arrows between them. Test it with 40 and 100 pages. Then write your own scenario that needs one choice, and swap. If you have a computer or tablet, draw the flowchart in a drawing program.
Practice
1
Trace the flowchart with these inputs (stock, sold): (40, 33), (20, 18) and (30, 25).
(40, 33): left = 7, which is not less than 5, so ‘Enough’. (20, 18): left = 2, which is less than 5, so ‘Order more’. (30, 25): left = 5, which is not less than 5, so ‘Enough’. The last one is the boundary value.
2
The user types 2 and then 3 in two separate runs. What does the program show each time?
Python
1distance = int(input("Km to school? "))
2if distance <= 2:
3print("Walk")
4else:
5print("Take the bus")
For 2 it shows Walk, because 2 <= 2 is true. For 3 it shows Take the bus, because 3 <= 2 is false.
3
Break this scenario into inputs, process, condition and outputs, and draw the flowchart: ‘A cook enters the number of eggs in a box. If there are fewer than 6, the program says Buy more; otherwise it says OK.’
Input: the number of eggs. Process: none. Condition: eggs < 6? Outputs: Buy more (yes) and OK (no). Flowchart: Start, INPUT eggs, diamond eggs < 6?, yes to OUTPUT Buy more, no to OUTPUT OK, Stop.
4
Explain why the test data 500 and 500 is a good test for the bottle program.
500 and 500 gives left = 0, which sits exactly on the edge of the condition left > 0. If the programmer had used >= by mistake, this test would show the bug.