Why am I getting KeyError: 0
Why am I getting KeyError: 0
我正在尝试编写 Conway 的 Game of Life。据我所知,程序应该是完整的,但是当我 运行 它时,我得到 KeyError: 0,据我所知,这意味着引用了一个不存在的字典键。字典 (cell_dict) 包含构成 dead/alive 单元格的 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 n != 0 or m != 0:
cell_neighbor = cell_dict[i+n][j+m]
if cell_neighbor['bg'] == "yellow":
pop += 1
if cell['bg'] == "yellow" and (pop == 2 or pop == 3): #primes the new status for the cell
cell['fg'] = "yellow"
elif cell['bg'] == "black" and pop == 3:
cell['fg'] = "yellow"
else:
cell['fg'] = "black"
for i in range(0,20): #updates the status of each cell
for j in range(0,20):
cell = cell_dict[i][j]
if cell['fg'] == "black":
cell['bg'] = "black"
elif cell['fg'] == "yellow":
cell['bg'] = "yellow"
window.after(1000,step) #repeats the process every second
#gui
window = tkinter.Tk()
cell_dict = {} #primes the dictionary
for i in range(0,400): #creates all the cells
cell = tkinter.Button(fg = "white", bg = "white", height = 1, width = 2)
r = random.randrange(0,2) #randomly determines if the cell will be initially dead or alive
if r == 0:
cell['bg'] = "black" #dead
elif r == 1:
cell['bg'] = "yellow" #alive
cell.grid(row = int(i/20)%20, column = i%20) #packs the cell into the grid
cell_dict[(int(i/20)%20,i%20)] = cell #stores the cell in the dictionary
#mainloop
step()
window.mainloop()
我注意到一旦弹出键错误,i = 399 并且 j 未定义。因此,代码似乎以某种方式跳过了“for i in range(0,2): for j in range(0,20):”。我是编码新手,所以我不确定哪里出错了。
你没有包括回溯,所以这只是一个有根据的猜测,但我怀疑你的问题写得更清楚:
cell_dict = {}
for i in range(0,400):
cell_dict[(int(i/20)%20,i%20)] = None
print(cell_dict)
for i in range(0,20):
for j in range(0,20):
cell = cell_dict[i][j] # key error happens here
事实上,您的密钥确实存在错误。
您的密钥是 cell_dict[(x, y)] -> cell
,但您本应拥有 cell_dict[x][y] -> cell
:
您可以通过多种方式解决该问题,在循环中分配字典,使用如下所示的 setdefault
或 collections.defaultdict
等
cell_dict.setdefault(int(i/20)%20, {})[i%20] = None
您似乎将单元格位置存储为两个整数的元组。
所以你应该用那个而不是整数来解决cell_dict。
使用 cell_dict[(i, j)]
而不是 cell_dict[i][j]
。
我正在尝试编写 Conway 的 Game of Life。据我所知,程序应该是完整的,但是当我 运行 它时,我得到 KeyError: 0,据我所知,这意味着引用了一个不存在的字典键。字典 (cell_dict) 包含构成 dead/alive 单元格的 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 n != 0 or m != 0:
cell_neighbor = cell_dict[i+n][j+m]
if cell_neighbor['bg'] == "yellow":
pop += 1
if cell['bg'] == "yellow" and (pop == 2 or pop == 3): #primes the new status for the cell
cell['fg'] = "yellow"
elif cell['bg'] == "black" and pop == 3:
cell['fg'] = "yellow"
else:
cell['fg'] = "black"
for i in range(0,20): #updates the status of each cell
for j in range(0,20):
cell = cell_dict[i][j]
if cell['fg'] == "black":
cell['bg'] = "black"
elif cell['fg'] == "yellow":
cell['bg'] = "yellow"
window.after(1000,step) #repeats the process every second
#gui
window = tkinter.Tk()
cell_dict = {} #primes the dictionary
for i in range(0,400): #creates all the cells
cell = tkinter.Button(fg = "white", bg = "white", height = 1, width = 2)
r = random.randrange(0,2) #randomly determines if the cell will be initially dead or alive
if r == 0:
cell['bg'] = "black" #dead
elif r == 1:
cell['bg'] = "yellow" #alive
cell.grid(row = int(i/20)%20, column = i%20) #packs the cell into the grid
cell_dict[(int(i/20)%20,i%20)] = cell #stores the cell in the dictionary
#mainloop
step()
window.mainloop()
我注意到一旦弹出键错误,i = 399 并且 j 未定义。因此,代码似乎以某种方式跳过了“for i in range(0,2): for j in range(0,20):”。我是编码新手,所以我不确定哪里出错了。
你没有包括回溯,所以这只是一个有根据的猜测,但我怀疑你的问题写得更清楚:
cell_dict = {}
for i in range(0,400):
cell_dict[(int(i/20)%20,i%20)] = None
print(cell_dict)
for i in range(0,20):
for j in range(0,20):
cell = cell_dict[i][j] # key error happens here
事实上,您的密钥确实存在错误。
您的密钥是 cell_dict[(x, y)] -> cell
,但您本应拥有 cell_dict[x][y] -> cell
:
您可以通过多种方式解决该问题,在循环中分配字典,使用如下所示的 setdefault
或 collections.defaultdict
等
cell_dict.setdefault(int(i/20)%20, {})[i%20] = None
您似乎将单元格位置存储为两个整数的元组。 所以你应该用那个而不是整数来解决cell_dict。
使用 cell_dict[(i, j)]
而不是 cell_dict[i][j]
。