I am a beginner in python programming, and I would be very thankful for some help with this problem. Most likely, it is something very simple I am doing wrong.

I have been given the task of programming a version of Conway's Game of Life. Most of it seems to be fairly straightforward, but I can't seem to get the neighbour count correct.

All cells in my grid have a value of 1 or 0. What I want to do is count how many 1's are around each cell, and then set a new value for the middle cell according to this total (1 stays 1 if surrounded by 2 or 3, 0 becomes 1 if surrounded by 3, any other value sets it to 0). I am probably making a very basic mistake here, but I have been staring myself blind at the code. Here is the entire method for calculating new values for each cell:

def generation(cell): cell_new = cell p = win.getMouse() for x in range(30): for y in range(30): neighbour_count = 0 for n in (x-1, x, x+1): for m in (y-1, y, y+1): try: if m == 1: neighbour_count = neighbour_count + 1 except IndexError, e: pass neighbour_count = neighbour_count - cell[x][y] #Takes the middle cell out of the calculation if neighbour_count == 3: cell_new[x][y] = 1 elif neighbour_count == 2 and cell[x][y] == 1: cell_new[x][y] = 1 elif neighbour_count != 2 and neighbour_count != 3: cell_new[x][y] = 0 return cell_new