tags:

views:

588

answers:

4

I am working on Conway's Game of Life currently and have gotten stuck. My code doesn't work.

When I run my code in 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
    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

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 at Python, and I don't know how to fix the error. I am a little bit confused about import "neighbours" to function "apply_rules", is that right way to do this?

+2  A: 

A cursory glance shows that your number_neighbors indices are off.

Also, you never initialize neighbors.

Response 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, which means add 1 to neighbors, but you never told it to start at 0, so it doesn't know what do add 1 to.

Also, notice the first and 3rd lines are exactly the same. I'm pretty sure this is not what you intended. That's what I meant by "your indices are off".

Response to Comment 2: apply_rules has several lines where you want to assign a value to something (which is '='), but you use '==' instead.

Jimmy
sorry about my english..i dont quite understand..but seems your are right(by feeling:)but what should i do?thanks
NONEenglisher
thanks alot!:)but after i add neighbours=0,although the code works without error but it doesnt turn to what i want..(just shown a array which the test code given),is that something wrong with the parameters(neighbours) in function apply_rules?
NONEenglisher
A: 

This is an extremely low-grade lazy question, but your number_neighbours function is broken, it checks universe_array[iy,ix-1] twice (and hence omits a check it should be doing).

Will Dean
Ehh...I don't know that it would be a *lazy* question for a brand new beginner. I think that may be a little harsh.
EBGreen
I think that may be the case too, but by definition, everything is beyond a beginner's skillset. Expanding your skillset is how you stop being a beginner.
EBGreen
thanks EBGreen,and i didnt just copy and paste the code...i wrote it on my own with a bit of helps from my friend.
NONEenglisher
At the point I answered the question, it didn't contain any details of what was failing - it basically said 'it doesn't work, please check it'
Will Dean
+3  A: 

You're trying to increment a variable that doesn't exist yet. Python can't increment something if it doesn't know what is. Try adding the following line at the top of your def number_neighbours function.

neighbours = 0
David Locke
but after i add neighbours=0,although the code works without error but it doesnt turn to what i want..(just shown a array which the test code given),is that something wrong with the parameters(neighbours) in function apply_rules?thank you very much:)
NONEenglisher
+11  A: 

Well, I guess that you are also quite new to programming per se, otherwise you should not have any problems in interpreting that simple error message.

I'll help you dissect it:

  • First, all "current" line numbers of your project's files are displayed, in calling order.
  • Then, it shows you the function in which the error occured: number_neighbours
  • Then, it shows you the contents of the line that contains the error: neighbours+=1
  • Finally, it tells you what the problem with that line is: UnboundLocalError: local variable 'neighbours' referenced before assignment

Now, what does that mean? Let's see what the += operator does: it adds something to the current value of neighbours. That means that it reads the current value, adds something to it, and finally stores it back. "Reading" is called "reference" with respect to variables.

What is the current value of neighbours? Well, it has never been used before, so it doesn't have any value -- there has never been a value assigned to it. Adding something to 'no value' is not a sensible thing to do. I guess that you expect it to have the value 0, but you have to tell it to your interpreter. To do this, add the following statement before, at the start of your function: neighbours = 0

Svante
+1 for the break down.
EBGreen
but after i add neighbours=0,although the code works without error but it doesnt turn to what i want..(just shown a array which the test code given),is that something wrong with the parameters(neighbours) in function apply_rules?thanks alot for your patient and skillful answer.
NONEenglisher
One more thing that jumps to the eye is that you do not look at the complete range in apply_rules. Arrays begin their index at 0, so you should use 'range(0,height-1)'. You also have to check for the case that you have an edge cell in number_neighbours.
Svante
@Harleqin: the range is correct if the boundaries are ignored but present so that the neighbor-counting code doesn't have to handle edge cases. you're actually playing 2x2 Life though.
Jimmy
If the boundaries are to be ignored, it would have to be 'range(1,height-2)', no?
Svante