Formulas, comparisons and loops
Answers are for parents and teachers.
1
Write the perimeter line for a rectangle with length 7 and width 2 and say the result.
perimeter = 2 * (7 + 2) gives 18.
2
Change the larger-of-two program so that it shows the smaller number. Which line changes, and what does it show for 12 and 5?
Python
1a = int(input("First number: ")) 2b = int(input("Second number: ")) 3if a > b: 4 print(a) 5else: 6 print(b)
Change if a > b: into if a < b:. For 12 and 5 the condition 12 < 5 is false, so the else branch shows 5.
3
Fill the trace table for total = 0 and for i in range(1, 4): total += i. How many rows are there, and what is the final total?
| A | B | |
|---|---|---|
| 1 | i | total after adding |
| 2 | 1 | |
| 3 | 2 | |
| 4 | 3 |
Three rows: i = 1, total 1; i = 2, total 3; i = 3, total 6. The final total is 6.
4
Mona’s marks are 45, 60 and 52. The rule is: pass if the average is at least 50. What does the idea of the program show, and who decided the rule?
The average is 157 / 3, about 52.3, which is at least 50, so it shows Pass. A person decided the rule; the program only follows it.
5
A friend writes for i in range(1, 5) to add the numbers 1 to 5. What goes wrong and how do you fix it?
range(1, 5) stops before 5, so only 1 to 4 are added (total 10). Write range(1, 6).