Skip to content
ExamSpine

Lesson 5.13 · Trace it

Trace: column totals

Python code16 rows to fill3 marks

Complete the trace table for this Python code. grid has 3 rows of 2 values, and grid[r][c] is the value in row r, column c. The outer loop goes along the columns and the inner loop goes down the rows. Then answer the question below the table. [3]

Line 1: grid = [[4, 7], [9, 3], [6, 8]]
Line 2: for c in range(len(grid[0])):
Line 3, indented 1 level: total = 0
Line 4, indented 1 level: for r in range(len(grid)):
Line 5, indented 2 levels: total = total + grid[r][c]

Fill in one row each time a line changes c, r 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.

Rowcrtotal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16