← Back to course
Python 11-12 / Week 05 / Monday
1/6
Week 05 · Grids

Monday

The field as a grid
// The test field as rows and columns
⏱ about 30 min

Monday: The Field as a Grid

Comet has drawn the Larkspur test field as five strings, one per row. "Pip picks up the sample at row 0, column 2," she says. "So I change that star to a dot."

She types one line in the shell, and it stops with a TypeError.

Wren leans in. "What does the evidence say?" he asks. "The message says the string will not take a new character. We need a field we can change."

Nova projects the field as a stack of small square cells. "How can I help?" she asks. "Try turning each row into a list. Then point at a cell with two numbers."

"Lead programmer," Comet says, "which number comes first?"

Five strings, one field

The Larkspur test field is 5 rows by 6 columns. A dot is open ground, # is a rock, and * is a sample spot.

This is made-up data from the FieldSim story. Here is Comet's first try in the shell:

>>> row = "..*..#"
>>> row[2]
'*'
>>> row[2] = "."
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: 'str' object does not support item assignment
READ THE SHELL
  • Read the question.
  • Tap your answer.
What does row[2] show?
What happens on row[2] = "."?

A list of lists

A 2D grid can be stored as a list of lists. Each inner list is one row, and each item in a row is one cell.

In a grid, the first index is the row and the second is the column. So cells[2][4] means row 2, column 4.

# field_list.py
FIELD = [
    "..*..#",
    ".#....",
    "...#*.",
    "*.....",
    "..#..*",
]

cells = []
for row in FIELD:
    cells.append(list(row))

print(len(cells), "rows")
print(len(cells[0]), "columns")
print(cells[0])
print(cells[2][4])
print(cells[4][2])
PREDICT FIELD_LIST.PY
  • Read the question.
  • Tap your answer.
What does print(len(cells), "rows") show?
What does print(cells[2][4]) show?
What does print(cells[4][2]) show?
5 rows
6 columns
['.', '.', '*', '.', '.', '#']
*
#
FIND CELLS[3][0], STEP BY STEP
  • ?Take item 3 of cells: the row list for "*.....".
  • ?Start at the outer list, cells.
  • ?The cell holds *, a sample spot.
  • ?In that row list, take item 0: the first cell.
WHY THIS EXERCISEcells[3] gives a whole row, and [0] then picks one cell from it.
StatementTrue or false?
In cells[r][c], the row comes first.?
cells[2][4] and cells[4][2] are the same cell.?
After list(row), one cell of that row can be changed.?
The test field has 6 rows.?
WHY THIS EXERCISEThe field is 5 rows by 6 columns, and the first index always picks the row.
At your computer
1. In your fieldsim folder, make field_list.py. Type it, save it and press F5. Check the five lines.
2. Add print(cells[3][0]). Predict it, then run it.
3. Add cells[0][2] = "." and then print(cells[0]). Run it and find the dot where the star was.
4. Change print(cells[4][2]) to print(cells[2][6]). Read the IndexError calmly: row 2 has no column 6. Then change it back.

Row first, then column. You will use that rule all week. Tomorrow you will walk every cell with nested loops.