1
我知道这个问题不是很精确,那是因为我不知道发生了什么。我已经将问题缩小到了几行,但对我来说执行起来似乎很奇怪。为什么我的代码给变量赋予错误的值?
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
值总是相同的。我不明白这是怎么回事,因为墙壁上的迷宫在test
之后被分配到maze
,而在那个地方没有墙壁。 根据调试,test
也包含第三行已被执行后的墙壁。 请帮忙,我非常困惑。
您需要显示真正的“placewalls”代码。很明显,“某些错误”是真实的,以便它返回第一个参数。 –
我确信某些错误是错误的,它实际上会返回正确的迷宫,包括墙壁。请记住,备份没有墙。 – Wh1spr
正如@Daniel所说的那样,太少的代码实际上说错了什么。但是,您是否会困惑Python如何传递变量(以及哪些变量)? “迷宫”和“测试”可能是同一个对象,但现在只是墙壁。例如参见[Python FAQ](https://docs.python.org/3/faq/programming.html#why-did-changing-list-y-also-change-list-x)。 – stephan