Conway Python implementation issue
I am currently working on Conway Game of Life and am stuck. My code doesn't work.
When I run my code in the GUI it says:
[[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 neighbors = number_neighbors (universe_array, iy, ix) File "C: \ Users \ Documents \ Physics \ Python \ MainProject \ conway.py", line 36, in number_neighbors neighbors + = 1 UnboundLocalError: local variable 'neighbors' referenced before assignment
Here is my 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<2:
evolved_array[iy,ix]==0
elif universe_array[iy,ix]==1 and neighbours>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
I am a beginner in Python and I have no idea how to fix the error. I'm a little confused about import "neighbours"
before function "apply_rules"
, is this the correct way to do it?
source to share
Well, I think you are also completely new to programming as such, otherwise you should have no problem interpreting this simple error message.
I'll help you dispel it:
- First, all the "current" line numbers of your project files are displayed in the order they are called.
- Then it shows you the function where the error occurred:
number_neighbours
- Then the contents of the line containing the error will be displayed:
neighbours+=1
- Finally, it informs you that the problem is with this line:
UnboundLocalError: local variable 'neighbours' referenced before assignment
Now what does this mean? Let's see what the operator does +=
: it adds something to the current value neighbours
. This means that it reads the current value, adds something to it, and finally saves it back. "Reading" is called "reference" relative to variables.
What is the current value neighbours
? Well, it has never been used before, so it has no value - no value has ever been assigned to it. Adding something to "no value" is not a smart thing to do. I am assuming that you expect it to be 0, but you must inform your interpreter about this. To do this, add the following statement before the start of your function:neighbours = 0
source to share
A quick glance reveals that your indexes are number_neighbors
off.
Also, you never initialize neighbors
.
Reply to comment:
def number_neighbours(universe_array,iy,ix):
if universe_array[iy,ix-1]==1:
neighbours+=1
if universe_array[iy,ix-1]==1:
neighbours+=1
if universe_array[iy+1,ix-1]==1:
neighbours+=1
You say neighbors +=1
what adding 1 to means neighbors
, but you never told him to start at 0, so he doesn't know what to add 1 to.
Also note that the first and third lines are exactly the same. I'm pretty sure this is not what you intended. This is what I mean by "your metrics are disabled".
Answer to comment 2:
apply_rules
Has multiple lines where you want to assign a value to something (which is '='), but you use '==' instead.
source to share