2 回答
TA贡献1869条经验 获得超4个赞
你从未填写过updated_grid,所以你不能分配给它的元素。
您应该在程序启动时创建两个网格。
def create_grid(ROW, COLUMN):
grid = []
for row in range(0, ROW):
grid2 = []
for column in range(0, COLUMN):
grid2.append(randint(0, 1))
grid.append(grid2)
return grid
grid = create_grid(ROW, COLUMN)
updated_grid = create_grid(ROW, COLUMN)
TA贡献1765条经验 获得超5个赞
最简单的解决方案是复制现有的网格并在以后使用该网格:
import copy
def apply_rules():
global grid
updated_grid = copy.deepcopy(grid)
# the rest of the function here, except the copying back again
# This is all that's needed to 'copy' it back again:
grid = updated_grid
这样,您从网格的副本开始:( copy.deepcopy(grid)) 并像您一样覆盖元素:( 例如updated_grid[row][column] = 0) 最后处理旧网格并将新网格保持在一行中:( grid = updated_grid) 通过引用计数的魔力.
这是一种形式double buffering。
添加回答
举报