Exam preparation · GCSE · about 15 min
Being the computer: a row for every change, and the checks that catch a slip.
[1 mark]In a trace table, when do you write a new row?
[1 mark]What does this program print?
total = 0
n = 4
while n > 0:
total = total + n
n = n - 1
print(total)10
4 + 3 + 2 + 1 = 10.
[1 mark]total = 0, n = 5. WHILE n > 1: total = total + n, n = n - 2. What is total at the end?
[1 mark]A loop ends when its condition is false. What should the trace show for that check?
[1 mark]What does 7 // 2 give?
Trace the algorithm in the comment by running it, printing one line per row in the form n=<n> total=<total> check=<true or false>, in the order the computer reaches them. Print the check as true while the loop keeps going and false on the row that ends it. Finish with output: <total>.
# the two lines every program starts with: the commands, then the robot from bugbot import * connect() # total = 0 # n = 5 # WHILE n > 1 # total = total + n # n = n - 2 # ENDWHILE # OUTPUT total total = 0 n = 5
The hint students can ask for: Print the row at the top of each pass, before changing anything, with the result of the check. The row that ends the loop is printed too, with the check false, and then the output.
from bugbot import *
connect()
total = 0
n = 5
while n > 1:
print(f"n={n} total={total} check=true")
total = total + n
n = n - 2
print(f"n={n} total={total} check=false")
print("output:", total)
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.