小编Sam*_*Sam的帖子

为什么我收到 KeyError: 0

我正在尝试编写康威的生命游戏代码。据我所知,程序应该完成,但是当我运行它时,我得到 KeyError: 0,据我了解,这意味着引用了不存在的字典键。字典 (cell_dict) 包含形成死/活单元格的 20x20 tkinter 按钮网格。

这是完整的代码:

#imports
import tkinter
import random

#functions
def step(): #updates the status of each cell
    for i in range(0,20): # runs through each cell to check the number of living neighbors
        for j in range(0,20):

            cell = cell_dict[i][j] #key error happens here
            pop = 0

            for n in range(-1,2): #checks each neighbor and tracks the number of alive ones
                for m in range(-1,2):
                    if i+n in range(0,20) and j+m in range(0,20):
                        if …
Run Code Online (Sandbox Code Playgroud)

python keyerror

2
推荐指数
1
解决办法
2万
查看次数

标签 统计

keyerror ×1

python ×1