Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem with Python implementation of Conway's Game of Life
    primarykey
    data
    text
    <p>I am working on <a href="http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life" rel="nofollow noreferrer">Conway's Game of Life</a> currently and have gotten stuck. My code doesn't work.</p> <p>When I run my code in GUI, it says:</p> <pre> [[0 0 0 0] [0 1 1 0] [0 1 0 0] [0 0 0 0]] Traceback (most recent call last): File "C:\Users\Documents\Physics\Python\MainProject\conway.py", line 53, in b= apply_rules(a) File "C:\Users\Documents\Physics\Python\MainProject\conway.py", line 14, in apply_rules neighbours=number_neighbours(universe_array,iy,ix) File "C:\Users\Documents\Physics\Python\MainProject\conway.py", line 36, in number_neighbours neighbours+=1 UnboundLocalError: local variable 'neighbours' referenced before assignment </pre> <p>Here is my code:</p> <pre><code>'''If a cell is dead at time T with exactly three live neighbours, the cell will be alive at T+1 If a cell is alive at time T with less than two living neighbours it dies at T+1 If a cell is alive at time T with more than three live neighbours it dies at T+1 If a cell is alive at time T with exactly two or three live neighbours it remains alive at T+1''' import numpy def apply_rules (universe_array): height, width = universe_array.shape # create a new array for t+1 evolved_array = numpy.zeros((height, width),numpy.uint8) for iy in range(1, height-1): for ix in range(1,width-1): neighbours=number_neighbours(universe_array,iy,ix) if universe_array[iy,ix]==0 and neighbours==3: evolved_array[iy,ix]==1 elif universe_array[iy,ix]==1 and neighbours&lt;2: evolved_array[iy,ix]==0 elif universe_array[iy,ix]==1 and neighbours&gt;3: evolved_array[iy,ix]==0 elif universe_array[iy,ix]==1 and neighbours==2 or neighbours==3: evolved_array[iy,ix]=universe_array[iy,ix] return evolved_array def number_neighbours(universe_array,iy,ix): neighbours=0 #fixed this line,thanks:) if universe_array[iy-1,ix-1]==1: neighbours+=1 if universe_array[iy,ix-1]==1: neighbours+=1 if universe_array[iy+1,ix-1]==1: neighbours+=1 if universe_array[iy-1,ix]==1: neighbours+=1 if universe_array[iy+1,ix]==1: neighbours+=1 if universe_array[iy-1,ix+1]==1: neighbours+=1 if universe_array[iy,ix+1]==1: neighbours+=1 if universe_array[iy+1,ix+1]==1: neighbours+=1 else: neighbours=neighbours return neighbours if __name__ == "__main__": a = numpy.zeros((4,4),numpy.uint8) a[1,1]=1 a[1,2]=1 a[2,1]=1 print a b= apply_rules(a) print b </code></pre> <p>I am a beginner at Python, and I don't know how to fix the error. I am a little bit confused about <code>import "neighbours"</code> to <code>function "apply_rules"</code>, is that right way to do this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload