2008-12-10 143 views
0

我目前正在致力于Conway's Game of Life并且陷入困境。我的代码不起作用。康威的生命游戏的Python实现问题

当我跑我的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",这是正确的方式吗?

+0

代码应该缩进并用空格分隔,以便进行格式化 – Jimmy 2008-12-10 23:19:15

+0

如果您准确解释了您的具体情况,则可能会得到更快的帮助被“卡住”。 – EBGreen 2008-12-10 23:20:29

+0

@EBGreen我在想同样的事情。 什么不按预期工作,你如何期望它工作? – UnkwnTech 2008-12-10 23:21:44

回答

13

好吧,我想你也是很新的编程本身,否则你不应该在解释如此简单的错误消息的任何问题。

我会帮你仔细研究它:

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

现在,这是什么意思?让我们来看看+=运营商做了什么:它增加了一些目前的值neighbours。这意味着它读取当前值,添加一些内容,最后将其存回。 “阅读”被称为变量的“参考”。

neighbours的当前值是多少?那么它以前从未使用过,所以它没有任何价值 - 从来没有分配过它的价值。将某些东西添加到“无价值”并不是一件明智的事情。我想你会期望它的值为0,但你必须把它告诉你的翻译。为此,请在函数开头之前添加以下语句:neighbours = 0

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,这意味着加1 neighbors,但你永远不告诉它在0开始,所以它不知道什么加1 。

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

回应评论2: apply_rules有几行,你要分配一个值的东西(这是“=”),但是你用“==”来代替。

+0

对不起我的英语..我不太明白..但看起来你是对的(感觉:)但我该怎么办?谢谢 – NONEenglisher 2008-12-10 23:35:10

+0

非常感谢!:) 但我添加邻居= 0后,虽然代码工作没有错误,但它不转向我想要的..(刚才显示的测试代码给出的数组),是函数apply_rules中的参数(邻居)有问题吗? – NONEenglisher 2008-12-10 23:52:20

0

这是一个非常低级的懒惰问题,但是您的number_neighbours函数被破坏,它会检查universe_array [iy,ix-1]两次(因此忽略它应该执行的检查)。

3

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

neighbours = 0