[英]Problem with Python implementation of Conway's Game of Life
我目前正在从事Conway的《人生游戏》 ,现在被卡住了。 我的代码不起作用。
当我在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"
感到有些困惑,这是正确的方法吗?
好吧,我想您本身对编程还是很陌生 ,否则您在解释该简单错误消息时应该没有任何问题。
我将帮助您进行剖析:
number_neighbours
neighbours+=1
UnboundLocalError: local variable 'neighbours' referenced before assignment
这是什么意思? 让我们看一下+=
运算符的作用:它为neighbours
的当前值添加一些内容。 这意味着它将读取当前值,向其中添加一些内容,最后将其存储回去。 关于变量,“读取”称为“参考”。
neighbours
的当前价值是多少? 好吧,它以前从未使用过,因此它没有任何值-从未分配任何值。 为“无价值”添加一些东西不是明智的选择。 我猜您希望它的值为0,但是您必须将其告诉解释器。 为此,请在函数开始之前添加以下语句: neighbours = 0
您正在尝试增加尚不存在的变量。 如果不知道Python是什么,Python就无法增加它。 尝试在def number_neighbours函数的顶部添加以下行。
neighbours = 0
粗略浏览一下即可发现您的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
有几行要在其中分配值(“ =”),但改用“ ==”。
这是一个非常低级的懒惰问题,但是您的number_neighbours函数已损坏,它两次检查Universe_array [iy,ix-1](因此省略了它应该执行的检查)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.