Attribute error: Object has no attribute Python
The issue appears to be that your for row in cells
loop is inside of your previous for x in range(0, self.sizex)
loop. Here's what the step
method should look like if you get it correctly indented:
def step (self): cells = self.cells for x in range (0,self.sizex): if x==0: x_down = self.sizex-1 else: x_down = x-1 if x==self.sizex-1: x_up = 0 else: x_up = x+1 for y in range(0,self.sizey): if y==0: y_down = self.sizey-1 else: Y_down = y-1 if y==self.sizey-1: y_up = 0 else: y_up = y+1 sum = cells[x_down][y].state + cells[x_up][y].state + cells[x][y_down].state + cells[x][y_up].state + cells[x_down][y_down].state +cells[x_up][y_up].state + cells[x_down][y_up].state + cells[x_up][y_down].state cells[x][y].setNextState(sum) for row in cells: # unindent these for cell in row: # lines by one cell.stepToNextState() # level each
If all the indentation issues are taken care of (or were not in the original code), there's still an issue that may cause an issue in certain situations. The issue is that the Cell.setNextState
method doesn't handle every situation. Specifically, it doesn't set nextState
if a cell is alive and should stay so (it has two or three living neighbors). The lack of an else
on your chain of if
and elif
statements should have raised this as a red flag for me, but I overlooked it the first time I examined the function.
Here's how it can be fixed:
def setNextState (self , Neighbours): if self.state == Cell.Live and (Neighbours < 2 or Neighbours > 3): self.nextState = Cell.Dead elif self.state == Cell.Dead and Neighbours == 3: self.nextState = Cell.Live else: # remove the conditions on this block, all the state changes were handled above self.nextState = self.state