Planning and testing code
Answers are for parents and teachers.
1
Draw a flowchart for a program that asks for the number of laps a runner ran and the length of one lap in metres, and shows the total distance.
Start, INPUT laps, INPUT lap_length, distance = laps * lap_length, OUTPUT distance, Stop. Each step is in the right shape, with arrows between them.
2
Trace this flowchart for the inputs 7 and 3. Write the trace table.
a = 7, b = 3, then diff = 4, and the output is 4. Trace table: INPUT a: a = 7. INPUT b: a = 7, b = 3. diff = a - b: a = 7, b = 3, diff = 4.
3
Work these out by hand and then say what Python shows: (a) 6 + 4 * 3 (b) (6 + 4) * 3 (c) 24 / 4 / 2 (d) 3 ** 2 + 1.
(a) 18, (b) 30, (c) 3.0, (d) 10. In (c) the divisions go from left to right, giving 6.0 and then 3.0.
4
A test plan expects 12 for the data 3 and 4 in a program that should multiply them. The program shows 7. Write the test plan row and say what the bug is.
Test 1, test data: num1 = 3, num2 = 4, expected outcome: 12, actual outcome: 7, fail. The bug is a logic error: the program adds the numbers instead of multiplying them (+ instead of *).
5
Find the mistake in each program, say what it is, and write the correct version: (a) colour = input("Colour? ) (b) size = 5, then on the next line print("Size:", sise) (c) print "Done"
(a) The closing quotation mark is missing: colour = input("Colour? "). (b) The variable is called size, not sise, so Python reports a NameError: print("Size:", size). (c) print needs brackets around the text: print("Done").