GoalCompare two programs by checking each one against the requirements, and say which is more robust.
New words
requirement · one job that a program has to do, written down before the program is maderobust · reliable: less likely to give a silly answer or crash when the input is oddcompare · to put two things side by side and look for the differences that matter
Explanation
Two programmers can solve the same problem in very different ways, and both programs may work. To judge them fairly, start with the requirements, which are the jobs the program must do. Read the requirements one at a time and find the line of each program that does the job. A program that leaves out a requirement is not a correct solution. Some programs also do extra work. Code that checks the input before using it makes a program robust, because it stops a silly result or a crash. An extra feature only counts as better when it helps the user or protects the program.
List the requirements→Find the line for each one→Tick or cross each one→Look for helpful extras
A converter. The requirements are: 1. ask for a temperature in degrees Celsius; 2. output the same temperature in degrees Fahrenheit (multiply by 9, divide by 5, add 32). Program A does this. The user types 100, and the program prints 212.0. Line 1 meets requirement 1, and lines 2 and 3 meet requirement 2.
Program B meets the same two requirements and adds a check. Nothing can be colder than about −273 °C, so a lower number is not a real temperature. The user types −300, and B prints a message. A would print In F: -508.0 for the same input, which looks like an answer but is impossible. This extra check makes B more robust.
Check
Program A
Program B
Program C
1. Asks for the temperature
yes
yes
yes
2. Outputs the Fahrenheit value
yes
yes
no
Extra: rejects impossible input
no
yes
no
Verdict
correct
correct and robust
not correct
A checklist for three programs. Program C is a third attempt: it prints the temperature it was given and never converts it. Reading the table, A and B meet both requirements, and B has a helpful extra. C fails requirement 2, so C is not a correct solution.
Class activity
House drawers (no computer). Write three requirements for a drawing on a card: 1. a square house; 2. a triangle roof on top; 3. a door in the middle of the bottom edge. Each person writes numbered instructions for it, such as ‘draw a square with sides 4 cm’, without showing them. Swap sheets and follow the other person’s instructions exactly. Tick each requirement on the card. Then add one robust extra to your instructions, for example ‘if the pencil line is too faint, go over it again’, and decide whether it really helps. (If you have a computer or tablet, you can type program A and program B from this lesson and try the inputs 20 and −300.)
Practice
1
The requirements are: 1. ask for the price in cents; 2. ask for the amount paid in cents; 3. output the change. Which requirements does this program meet? What does it print when the price is 250 and the amount paid is 200? Is that a good result?
Python
1price = int(input("Price in cents? "))
2paid = int(input("Paid in cents? "))
3print("Change:", paid - price)
It meets all three requirements: lines 1 and 2 ask for the two numbers and line 3 outputs the change. For 250 and 200 it prints Change: -50. That is a silly answer, because the customer has not paid enough, so the program is not robust.
2
Fix the problem in the previous program. Write the lines that make it robust, so that it outputs Not enough money when the amount paid is less than the price.
Model answer (lines are separated by /, and [indent] means four spaces at the start of the line): price = int(input("Price in cents? ")) / paid = int(input("Paid in cents? ")) / if paid < price: / [indent] print("Not enough money") / else: / [indent] print("Change:", paid - price). Check: For 250 and 200 it prints Not enough money. For 250 and 300 it prints Change: 50.
3
The requirement is: ask for a number of minutes and output the number of seconds. The user types 5. Trace this program. Does it meet the requirement?
The program prints 65, because 5 + 60 = 65. The correct answer is 5 × 60 = 300. It does not meet the requirement: it has a logic error, because it adds instead of multiplying.
4
This program shares 12 sweets between friends. What does it print when the user types 4, and what happens when the user types 0? Write a version that is robust.
Model answer (lines are separated by /, and [indent] means four spaces at the start of the line): friends = int(input("How many friends? ")) / if friends > 0: / [indent] print("Each gets", 12 / friends) / else: / [indent] print("Need at least 1 friend"). Check: The original prints Each gets 3.0 for 4, and for 0 it stops with ZeroDivisionError: division by zero. The robust version prints Need at least 1 friend for 0.