Python实现康威生命游戏的问题

时间:2008-12-10 23:17:03

标签: python

我目前正在Conway's Game of Life工作并且卡住了。我的代码不起作用。

当我在GUI中运行我的代码时,它说:

 
[[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

这是我的代码:

'''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

我是Python的初学者,我不知道如何修复错误。我对import "neighbours"function "apply_rules"有点困惑,这是正确的方法吗?

4 个答案:

答案 0 :(得分:13)

好吧,我想你对编程本身也是一个新手,否则你在解释这个简单的错误信息时应该没有任何问题。

我会帮你解剖一下:

  • 首先,按照调用顺序显示项目文件的所有“当前”行号。
  • 然后,它会显示发生错误的函数:number_neighbours
  • 然后,它会显示包含错误的行的内容:neighbours+=1
  • 最后,它会告诉您该行的问题是:UnboundLocalError: local variable 'neighbours' referenced before assignment

现在,这是什么意思?让我们看看+=运算符的作用:它为neighbours的当前值添加了一些内容。这意味着它会读取当前值,向其中添加内容,最后将其存储回来。 “阅读”在变量方面被称为“参考”。

neighbours的当前值是多少?好吧,它以前从未使用过,所以它没有任何价值 - 从来没有分配给它的值。添加“无价值”的东西不是明智之举。我想你希望它的值为0,但你必须告诉你的翻译。为此,请在函数开头之前添加以下语句:neighbours = 0

答案 1 :(得分:3)

您正在尝试增加尚不存在的变量。如果不知道是什么,Python就无法增加某些东西。尝试在def number_neighbours函数的顶部添加以下行。

neighbours = 0

答案 2 :(得分:2)

粗略一瞥显示您的number_neighbors指数已关闭。

此外,您永远不会初始化neighbors

对评论的回应:

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

你说,neighbors +=1,这意味着向neighbors添加1,但你从未告诉它从0开始,所以它不知道添加1是什么。

另外,请注意第一行和第三行完全相同。我很确定这不是你想要的。这就是我所说的“你的指数已关闭”。

对评论2的回应: apply_rules有几行您要为某些内容赋值(即'='),但您使用的是'=='。

答案 3 :(得分:0)

这是一个极低级别的懒惰问题,但是你的number_neighbours函数被破坏了,它检查了universe_array [iy,ix-1]两次(因此省略了它应该做的检查)。