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。
添加回答
舉報