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.
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:
8print("You can buy it")
9else:
10print("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.
Test
Test data
Expected outcome
Actual outcome
Pass/fail
1
name = Lola, weekly = 4, weeks = 6, price = 20
Lola has saved 24, You can buy it
Lola has saved 24, You can buy it
pass
2
name = Lola, weekly = 5, weeks = 3, price = 15
Lola has saved 15, You can buy it
Lola has saved 15, You can buy it
pass
3
name = Lola, weekly = 2, weeks = 4, price = 10
Lola has saved 8, Keep saving
Lola has saved 8, Keep saving
pass
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?
total >= 30?. The sign >= includes exactly 30, and the requirement says ‘30 or more’.
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.
Test
Test data
Expected outcome
1
mine = 12, friend = 25
?
2
mine = 7, friend = 9
?
3
mine = 20, friend = 10
?
Test 1: total 37, Album full. Test 2: total 16, Keep swapping. Test 3: total 30, Album full. Test 3 is the boundary test, because 30 sits exactly on the edge of the condition.
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.
The program shows 216, because line 3 multiplies instead of adding. It is a logic error. The correct line is total = mine + friend.
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.
Questions: Does it do everything that was asked? Would a stranger understand every message? Does each actual outcome match the expected outcome? Improvement: check that the user really typed a number, so that the program does not stop with an error.