Lesson 5.16 · Trace it
Trace: queue moves
Python code15 rows to fill3 marks
Complete the trace table for this Python code. queue is used as a queue: its first value is the front and its last value is the back. append adds a value at the back, and pop(0) takes the front value off and gives it back. Write the queue in square brackets, front first, as Python prints it. Then answer the question below the table. [3]
Line 1: queue = []Line 2: queue.append(3)Line 3: queue.append(8)Line 4: x = queue.pop(0)Line 5: queue.append(5)Line 6: queue.append(x + 4)Line 7: total = 0Line 8: while len(queue) > 0:Line 9, indented 1 level: front = queue.pop(0)Line 10, indented 1 level: if front < 6:Line 11, indented 2 levels: queue.append(front + 4)Line 12, indented 1 level: else:Line 13, indented 2 levels: total = total + front
Fill in one row each time a line changes queue, x or total. The table already has the right number of rows. In each row, write only what that line changes; you can leave the other cells empty or copy their values down. Write values the way Python shows them: 3 and 3.0 are different, and text goes in quotes.
| Row | queue | x | total |
|---|---|---|---|
| 1 | |||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 | |||
| 6 | |||
| 7 | |||
| 8 | |||
| 9 | |||
| 10 | |||
| 11 | |||
| 12 | |||
| 13 | |||
| 14 | |||
| 15 |