I am making Conway’s game of life in python, and I am a beginner. I decided to use 6 functions overall to implement it and here they are:

Function 1: create a blank grid

input: nothing

return: a blank grid

Function 2: print a given grid

input: a grid

return: nothing

Function 3: load a pattern

input: a file name, a grid

return: nothing

Function 4: advance a grid one generation

input: a grid

return: a new grid advanced by one generation

Function 5: advance a cell one generation

input: a row, a column, a grid

return: whether the cell is alive or not (True or False)

Function 6: determine the number of living neighbors of a cell

input: a row, a column, a grid

return: the number of living neighbors of the cell

Function 5 and 6 are really just helpers to 4 because you need 6 for 5, and 5 for 4. I have done 1, 2, 3, 5, and 6, and I am working on 4 right now, but I get an error when I run my code in function 6.

```
living_cell = "O"
dead_cell = "-"
def create_blank_grid():
# # line = (dead_cell for i in range(59))
# line = ()
# for i in range(59):
# line.append(dead_cell)
#
# # grid = (("-") * 30 for i in range(60))
# line.append("n")
# # grid = (line for i in range(30))
line = ()
for i in range(59):
line.append(dead_cell)
line.append("n")
grid = ()
for j in range(30):
grid.append(line(:))
# 11 31
# 11 32
# 12 31
# 12 33
# 13 32
return grid
# print(create_blank_grid())
def print_grid(grid):
for i in grid:
# print(len(i))
for j in i:
if j == "n":
print(" " * (69 - 59))
else:
print(j, end="")
return grid
# print_grid(create_blank_grid())
def load_design(file_name, grid):
myFile = open(file_name, "r")
coordinates = ()
for line in myFile:
real_line = line.split()
row, col = real_line(0), real_line(1)
coordinates.append((row, col))
myFile.close()
# turn on certain cells here
for row, col in coordinates:
row, col = int(row), int(col)
# print(row, col)
grid(row)(col) = living_cell
# print_grid(grid)
for i in grid:
# print(len(i))
for j in i:
print(j, end="")
# return grid
load_design("square.in", create_blank_grid())
# square.in just has coordinates (row, col) in str form like this:
# 11 31
# 11 32
# 12 31
# 12 32
def num_living_neighbors(row, col, grid):
# just to make sure
row = int(row)
col = int(col)
living_neighbors_count = 0
# if grid(row)(col + 1) == living_cell:
# living_neighbors_count += 1
# if grid(row)(col - 1) == living_cell:
# living_neighbors_count += 1
# if grid((row + 1) % len(grid))(col) == living_cell:
# living_neighbors_count += 1
# if grid(row - 1)(col) == living_cell:
# living_neighbors_count += 1
# if grid((row + 1) % len(grid))((col + 1) % len(grid)) == living_cell:
# living_neighbors_count += 1
# if grid((row + 1) % len(grid))((col - 1) % len(grid)) == living_cell:
# living_neighbors_count += 1
# if grid(row - 1)(col + 1) == living_cell:
# living_neighbors_count += 1
# if grid(row - 1)(col - 1) == living_cell:
# living_neighbors_count += 1
if (col + 1) < len(grid(row)) and grid(row)(col + 1) == living_cell:
living_neighbors_count += 1
if (col - 1) >= 0 and grid(row)(col - 1) == living_cell:
living_neighbors_count += 1
if (row + 1) < len(grid(row)) and grid(row + 1)(col) == living_cell:
living_neighbors_count += 1
if (row - 1) >= 0 and grid(row - 1)(col) == living_cell:
living_neighbors_count += 1
if (((col + 1) < len(grid(row))) and (row + 1) < len(grid(row))) and grid(row + 1)(col + 1) == living_cell:
living_neighbors_count += 1
if (((row + 1) < len(grid(row))) and (col - 1) >= 0) and grid(row + 1)(col - 1) == living_cell:
living_neighbors_count += 1
if (((row - 1) >= 0) and (col + 1) < len(grid(row))) and grid(row - 1)(col + 1) == living_cell:
living_neighbors_count += 1
if (((row - 1) >= 0) and (col - 1) >= 0) and grid(row - 1)(col - 1) == living_cell:
living_neighbors_count += 1
return living_neighbors_count
def adv_cell_one_gen(row, col, grid):
dead = ()
living = ()
# is alive, less than 2 alive neighbors
if grid(row)(col) == living_cell and num_living_neighbors(row, col, grid) < 2:
# grid(row)(col) = dead_cell
# return False
dead.append((row, col))
# is alive, 2 or 3 alive neighbors
if grid(row)(col) == living_cell and (
num_living_neighbors(row, col, grid) == 2 or num_living_neighbors(row, col, grid) == 3):
# grid(row)(col) = dead_cell
# return True
living.append((row, col))
# is alive, more than 4 alive neighbors
if grid(row)(col) == living_cell and num_living_neighbors(row, col, grid) > 4:
# grid(row)(col) = dead_cell
# return False
dead.append((row, col))
# is dead, has 3 alive neighbors
if grid(row)(col) == dead_cell and num_living_neighbors(row, col, grid) == 3:
# grid(row)(col) = dead_cell
# return True
living.append((row, col))
for i in grid:
for j in i:
if (i, j) in living:
print(living_cell)
elif (i, j) in dead:
print(dead_cell)
def adv_grid_one_gen(grid):
for i in range(len(grid)):
for j in range(i):
adv_cell_one_gen(i, j, grid)
# for i in range(len(grid)):
# for j in range(len(grid(i))):
# if adv_cell_one_gen(j, i, grid) == True:
# grid(i)(j) = living_cell
# else:
# grid(i)(j) = dead_cell
return grid
# print(adv_grid_one_gen(print_grid(create_blank_grid())))
adv_grid_one_gen(create_blank_grid())
```

When I run this I get this error:

```
Traceback (most recent call last):
File "C:/Users/Krish/python-masterclass/current-python-masterclass-remaster/HelloWorld/conway's game of life.py", line 231, in <module>
adv_grid_one_gen(create_blank_grid())
File "C:/Users/Krish/python-masterclass/current-python-masterclass-remaster/HelloWorld/conway's game of life.py", line 214, in adv_grid_one_gen
adv_cell_one_gen(i, j, grid)
File "C:/Users/Krish/python-masterclass/current-python-masterclass-remaster/HelloWorld/conway's game of life.py", line 196, in adv_cell_one_gen
if grid(row)(col) == dead_cell and num_living_neighbors(row, col, grid) == 3:
File "C:/Users/Krish/python-masterclass/current-python-masterclass-remaster/HelloWorld/conway's game of life.py", line 145, in num_living_neighbors
if (row + 1) < len(grid(row)) and grid(row + 1)(col) == living_cell:
IndexError: list index out of range
```

I know what this means, but I am unable to understand why I got it since I tried to check before indexing so I wouldn’t get an error, or in other words, I tried to prevent this error but I still got it.

I have no clue what to do, and I would appreciate it if someone could help me with this, and preferably write out some/all of function 6 since that is the root of my problems.