我只有一个(很可能是)最后一个问题。从我发送上一个问题开始,我已经取得了一些进步,到目前为止,我已经对5/6函数进行了编码(1、2、3、5和6),而函数4实际上只是将函数5和6放在一起。是我需要帮助的。到目前为止,这是我的代码:
"""
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)
功能1:create_blank_grid
功能2:print_grid
功能3:load_design
功能4:adv_grid_one_gen
功能5:adv_cell_one_gen
功能6:num_living_neighbors
我了解在adv_cell_one_gen函数中,我将row和col设置为整数(row = int(row)和col = int(col)),这会导致错误,但是如果我不这样做,则由于没有整数的索引,我将得到索引错误。我认为我的代码从根本上存在缺陷,我不知道在哪里,但是我认为自己处于死胡同。这是我的问题,希望您能提供帮助。
答案 0 :(得分:0)
在"".join(line)
中,line应该是字符串列表。
但是,如果我们看一下您的代码,您将追加living_cell
或dead_cell
,在前两行中将它们声明为布尔值。因此,line现在是布尔值列表,这是错误的输入类型。
我不确定您要在这里实现什么,但是如果您只想打印输出,则可以
line.append(str(living_cell))
line.append(str(dead_cell))
答案 1 :(得分:0)
由于您已将单元格定义为布尔值,因此出现该错误:
living_cell = True
dead_cell = False
''.join()
函数获取列表中的元素,并将它们作为单个字符串与您定义的分隔符连接在一起。分隔符是您在调用join()
之前立即在字符串中输入的任何内容。在这种情况下,其''
。
join()
仅适用于可迭代的完整字符串。您提供了一个可迭代的列表(您的line
列表),但是列表中充满了布尔值而不是字符串。
要克服此错误,您至少有三个选择:
这可能像这样简单:
living_cell = "True"
dead_cell = "False"
但是,我怀疑如果您的其他函数依赖于这些变量为布尔值才能正常工作,那么这将破坏其余的代码。
join
调用join()
函数时,可以使用列表推导将所有内容更改为字符串:
print("".join([str(cell) for cell in line])
如果此功能试图做的就是将电路板打印出来(看起来确实是这种情况),那么应该就可以完成工作。