Reputation: 55
I am attempting to code Conway's Game of Life. As far as I can tell the program should be complete, but when I run it I get KeyError: 0, which to my understanding means a nonexistent dictionary key was referenced. The dictionary (cell_dict) contains the 20x20 grid of tkinter buttons that form the dead/alive cells.
Here is the whole code:
#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 notice that once the key error pops, i = 399 and j is undefined. So it seems like the code is somehow skipping the "for i in range(0,2): for j in range(0,20):". I'm new to coding so I'm not sure where I went wrong.
Upvotes: 3
Views: 26889
Reputation: 4427
You didn't include the traceback, so this is just an educated guess, but I suspect your issue is more cleanly written like this:
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
And in fact you do have your key error there.
Your keys are cell_dict[(x, y)] -> cell
, but you meant to have cell_dict[x][y] -> cell
:
You can fix that in several ways, allocating dicts in your loop, using setdefault
as below, or collections.defaultdict
, etc.
cell_dict.setdefault(int(i/20)%20, {})[i%20] = None
Upvotes: 4
Reputation: 1251
it looks like you store cells locations as a tuple of two integers. so you should address cell_dict with that instead of an integer.
Use cell_dict[(i, j)]
instead of cell_dict[i][j]
.
Upvotes: 6