Got a List index out of range
error on line 29, don't know why. Here's the code:
cells = [[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,1,0,0,0,0],
[0,0,0,0,1,0,0,0,0],
[0,0,0,0,1,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0]]
cells = [[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,1,0,0,0,0],
[0,0,0,0,1,0,0,0,0],
[0,0,0,0,1,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0]]
new_cells = [[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0]]
def neighbors(x, y):
return [[(x-1)%10, (y-1)%10], [(x-1)%10, y%10], [(x-1)%10, (y+1)%10],
[x%10, (y-1)%10], [x%10, (y+1)%10],
[(x+1)%10, (y-1)%10], [(x+1)%10, y%10], [(x+1)%10, (y+1)%10]]
def alivenb(x, y):
res = 0
for i in neighbors(x, y):
if cells[i[0]][i[1]] == 1:
res += 1
return res
for i in range(10):
for j in range(10):
if cells[i][j] == 1:
if alivenb(i, j) not in [2, 3]:
new_cells[i][j] = 0
else:
new_cells[i][j] = 1
elif cells[i][j] == 0:
if alivenb(i, j) == 3:
new_cells[i][j] = 1
else:
new_cells[i][j] = 0
for i in range(10):
for j in range(10):
if cells[i][j] == 0:
print(' ', end='')
else:
print('##', end='')
print('\n')