Reputation:
I have just one (most likely) last question. From when I sent my last question, I have progressed a bit and I have coded 5/6 functions so far (1, 2, 3, 5, and 6), and function 4 is really just putting together functions 5 and 6, which is what I need help with. This is my code so far:
"""
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
"""
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[:])
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="")
# 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="")
# load_design("hertz-oscillator.in", create_blank_grid())
def num_living_neighbors(row, col, grid):
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][col] == living_cell:
living_neighbors_count += 1
if grid[row - 1][col] == 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 grid[row - 1][col + 1] == living_cell:
living_neighbors_count += 1
if grid[row - 1][col - 1] == living_cell:
living_neighbors_count += 1
return living_neighbors_count
def adv_cell_one_gen(row, col, grid):
row = int(row)
col = int(col)
# is alive, less than 2 alive neighbors
if grid[row][col] == living_cell and num_living_neighbors(row, col, grid) < 2:
return False
# 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):
return True
# is alive, more than 4 alive neighbors
if grid[row][col] == living_cell and num_living_neighbors(row, col, grid) > 4:
return False
# is dead, has 3 alive neighbors
if grid[row][col] == dead_cell and num_living_neighbors(row, col, grid) == 3:
return True
def adv_grid_one_gen(grid):
# for i in range(len(grid)):
# for j in range(i):
# adv_cell_one_gen(j, i, grid)
for i in grid:
for j in 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(create_blank_grid()))
adv_grid_one_gen(create_blank_grid)
Function 1: create_blank_grid
Function 2: print_grid
Function 3: load_design
Function 4: adv_grid_one_gen
Function 5: adv_cell_one_gen
Function 6: num_living_neighbors
My question here is that when I try to run this, I get this error:
I understand that in the adv_cell_one_gen function, I made row and col into integers (with row = int(row) and col = int(col)), and that is causing the error, but if I don't do this, then I would get an index error because of indexing without integers. I think there is somewhere else that my code is fundamentally flawed and I don't know where, but I think I am at a dead end. This was my question and I hope you can help.
Upvotes: 0
Views: 342
Reputation: 893
You're getting that error because you have defined your cells as booleans:
living_cell = True
dead_cell = False
The ''.join()
function takes the elements in a list and joins them together as a single string with whatever delimiter you define. The delimiter is whatever you put in the string immediately before you call join()
. In this case its ''
.
join()
only works on an iterable full of strings. You have provided an iterable (yourline
list) but its full of booleans instead of strings.
To get past this error you have at least three options:
This could be as simple as something like this:
living_cell = "True"
dead_cell = "False"
However, I suspect this will break the rest of your code if your other functions rely on these variables being booleans to function properly.
join
You could use a list comprehension to change everything to strings when you call the join()
function:
print("".join([str(cell) for cell in line])
If all this function is trying to do is print out the board (which appears to be the case) this ought to get the job done.
Upvotes: 1
Reputation: 302
In "".join(line)
, line should be a list of strings.
However, if we look at your code, you are appending living_cell
or dead_cell
, which you declared as booleans in the first two lines. Thus, line is now a list of booleans, which is the wrong input type.
I'm not sure what you want to achieve here, but if you just want to print the output, you can do
line.append(str(living_cell))
line.append(str(dead_cell))
Upvotes: 0