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

Flowcharts that choose

Week 4 · Unit 7.1: Block it out · 3 lessons
3

From story to flowchart

Computing 7: Unit 7.1, pp. 8–43
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.

StartINPUT sizeINPUT drunkleft = size - drunkleft > 0?yesnoOUTPUT "Keep drinking"OUTPUT "Well done"End
Worked examples
Part of the storyBecomes
enters the size and how much was drunkINPUT size, INPUT drunk
works out what is leftprocess: left = size - drunk
if something is leftcondition: left > 0?
Keep drinking / Well donetwo 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:
5    print("Keep drinking")
6else:
7    print("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.
sizedrunkleftleft > 0?Output
500200300yesKeep drinking
5005000noWell done
500620−120noWell 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).
StartINPUT stockINPUT soldleft = stock - soldleft < 5?yesnoOUTPUT "Order more"OUTPUT "Enough"End
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:
3    print("Walk")
4else:
5    print("Take the bus")
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.’
4
Explain why the test data 500 and 500 is a good test for the bottle program.