Consider the following Python program.
total = 0
for i in range(1, 6):
if i % 2 == 0:
total = total + i * i
else:
total = total - i
print(total)(a) Complete a trace table showing the value of total at the end of each iteration, and state the output. [3]
(b) State the output if range(1, 6) is changed to range(1, 5). [1]
- i = 1 → total = −1; i = 2 → total = 3(1)
- i = 3 → total = 0; i = 4 → total = 16(1)
- i = 5 → total = 11, so the output is 11(1)
- (b) 16(1)
- Answer: (a) −1, 3, 0, 16, 11; output 11 (b) 16
- Examiner tip: Odd values are subtracted, even values have their square added; check the sign on every row.