我知道这个问题不是很精确,这是因为我也不知道这是怎么回事。我已将问题缩小到几行,但对我来说,执行似乎很奇怪。
def loadmaze(backup):
test = createmaze(10,10) #This creates a new 10x10 matrix
maze = placewalls(test, "x") #The X is a string with wall positions
if maze != test:
#when placewalls executed correctly, and returned a maze different from the original
showmaze(maze) #simply printing maze to console
else:
return backup #so nothing changes, looks as if function never happened
return maze,10,10
# this returns the new maze, with walls, and the dimensions of the new maze
def placewalls(amaze,positions):
#placing walls, definitely correct
return amaze-with-walls
显然,我更改了变量名,因此很清楚我在做什么。问题是,当我调用该函数时loadmaze(maze)
,它永远不会返回带有墙的迷宫。test
和maze
值总是以某种方式相同。我不明白这是怎么回事,因为带墙壁的迷宫被分配给maze
after test
,那时在那个地方没有墙壁。根据调试,在执行第三行之后test
还包含墙。请帮助,我非常困惑。
正如@stephan所说,变量test
和maze
指向内存中的相同对象。所以我改变了maze = placewalls(test,"x")
用maze = placewalls(createmaze(10,10),"x")
,从而创造从迷宫中一个新的对象不同。然后,这导致测试和迷宫是两个不同的迷宫。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句