Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
Shade in the A or B circle for each question.
Draw a line connecting each item on the left to its match on the right.
| Left column | Right column | |
|---|---|---|
| 1for row in range(len(grid)): for col in range(len(grid[row])): | AThe indexes are swapped. grid[col][row] uses the column number as the row index, so the wrong cells are read or an index error occurs. It should be grid[row][col]. | |
| 2total += grid[row][col] | Bfor r in range(len(grid)): for c in range(len(grid[r])): if grid[r][c] == target: | |
| 3Print a grid row by row: the inner loop builds one row, then the outer loop moves to the next line | Cfor row in grid: for item in row: print(item, end=' ') print() | |
| 4Find a value in a grid by checking every element with nested loops | DAdds the current element to a running total while the nested loops visit each cell | |
| 5for row in range(3): for col in range(4): print(grid[col][row]) | Etotal is reset to 0 inside the outer loop and never saved, so each row total is lost and only the last row remains. Print or store each row total after the inner loop, before the next row begins. | |
| 6for row in range(len(grid)): total = 0 for col in range(len(grid[row])): total += grid[row][col] total_all = total | FOuter loop steps through each row and the inner loop steps through each column in that row, so every element is visited |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
grid[row][col] — Reads one element of a 2D array. An outer loop steps through each row and an inner loop steps through each column, so every element in the grid gets visited.for row in range(len(grid)):
for col in range(len(grid[row])):
print(grid[row][col])for row in range(3):
for col in range(3):
totals[col] += grid[row][col]for row in range(len(grid)):
for col in range(len(grid[row])):
print(grid[row][col], end=' ')for row in range(len(grid)):
for col in range(len(grid[row])):
if grid[row][col] == target:for row in range(len(grid)):
for col in range(len(grid[row])):
print(grid[row][col])