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

Sub-routines and a project

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

Plan, build, test, evaluate

Computing 7: Unit 7.1, pp. 8–43
GoalCarry out a small project from plan to evaluation: draw the flowchart, write the sequence part in Python, test with a test plan, and judge the result.
New words
requirement · something the program must doevaluate · to judge how well a program meets its requirements and what could be betterboundary test · a test with a value on the edge of a condition
Explanation

A project has four stages. First plan: write what the program must do, then draw a flowchart. Second build: write the Python for the input, the calculation and the output. Third test: make a test plan and decide the expected outcomes by hand before you run anything. Include a boundary test. Fourth evaluate: check that the program does everything it was asked to do, read its messages as a stranger would, compare every actual outcome with the expected one, and name one change that would make it better. The project in the picture is a pocket-money planner. It asks for a name, a weekly amount, a number of weeks and the price of a toy.

StartINPUT nameINPUT weeklyINPUT weeksINPUT pricesaved = weekly * weeksOUTPUT name, "has saved", savedsaved >= price?yesnoOUTPUT "You can buy it"OUTPUT "Keep saving"End
Worked examples
Python
1name = input("Your name? ")
2weekly = int(input("Pocket money each week? "))
3weeks = int(input("Weeks of saving? "))
4price = int(input("Price of the toy? "))
5saved = weekly * weeks
6print(name, "has saved", saved)
7if saved >= price:
8    print("You can buy it")
9else:
10    print("Keep saving")
Output
Lola has saved 24
You can buy it
This is the complete planner in Python, ready to read. The first six lines are the sequence that you can write yourself. The if and else lines are the selection from the flowchart. The user types Lola, 4, 6 and 20.
TestTest dataExpected outcomeActual outcomePass/fail
1name = Lola, weekly = 4, weeks = 6, price = 20Lola has saved 24, You can buy itLola has saved 24, You can buy itpass
2name = Lola, weekly = 5, weeks = 3, price = 15Lola has saved 15, You can buy itLola has saved 15, You can buy itpass
3name = Lola, weekly = 2, weeks = 4, price = 10Lola has saved 8, Keep savingLola has saved 8, Keep savingpass
This is the test plan. The expected outcomes were worked out by hand before the program was run. Test 2 is the boundary test, because 15 is exactly the price.
Now evaluate. The planner meets every requirement and the messages are easy to read. It could be improved. What happens if the user types 0 weeks? It says Keep saving, which is right. What if the user types a word instead of a number? The program stops with a ValueError. A better version would check the input.
Class activity

Pair project (no computer): invent a small program that uses two inputs, one calculation and one choice. Ideas: how many trays of cookies fill 3 boxes, whether a bus has enough seats for a class, or whether a team has scored enough points to reach the final. Step 1: write the requirements and draw the flowchart. Step 2: write the Python for the inputs, the calculation and the first print line. Step 3: write a test plan with three tests, including a boundary test. Step 4: swap with a partner, who works out the actual outcomes by hand, and then answers the four evaluation questions. If you have a computer or tablet, type your program into IDLE with an adult.

Practice
1
The flowchart should output ‘Album full’ for 30 stickers or more. Which condition goes in the diamond?
StartINPUT nameINPUT mineINPUT friendtotal = mine + friendOUTPUT name, "has", total, "stickers"?yesnoOUTPUT "Album full"OUTPUT "Keep swapping"End
2
Work out the expected outcome of each test by hand. The sticker program adds two numbers and says ‘Album full’ for 30 or more, and ‘Keep swapping’ otherwise.
TestTest dataExpected outcome
1mine = 12, friend = 25?
2mine = 7, friend = 9?
3mine = 20, friend = 10?
3
Test 1 expects a total of 30 when the user types 12 and 18. The program shows something else. Find the bug and write the correct line.
Python
1mine = int(input("Your stickers? "))
2friend = int(input("Friend's stickers? "))
3total = mine * friend
4print("Total:", total)
Output
Total: 216
4
Write three questions you would ask when you evaluate a partner’s program, and one improvement that you would suggest to a program that asks for a number.