python 函数中的变量 += 1 错误地加了 2
variable += 1 in python function adding incorrectly by 2
我在 class 中有这个函数 move()
函数,它在包含 self.coords[1] += 1
的行中错误地添加了 2 而不是 1
这是文件:
class Actor():
def __init__(self, start, limits, barriers):
self.coords = start
self.limits = limits
self.barriers = barriers
def canmove(self, direction):
moving = self.coords
if(direction == 'up'):
moving[1] -= 1
elif(direction == 'right'):
moving[0] += 1
elif(direction == 'down'):
moving[1] += 1
elif(direction == 'left'):
moving[0] -= 1
if((moving[0] > self.limits[0]) or (moving[1] > self.limits[1]) or (-1 in moving) or (moving in self.barriers)):
return False
else:
return True
def move(self, direction):
if(direction == 'up'):
if self.canmove('up'):
self.coords[1] -= 1
elif(direction == 'right'):
if self.canmove('right'):
self.coords[0] += 1
elif(direction == 'down'):
if self.canmove('down'):
self.coords[1] += 1
elif(direction == 'left'):
if self.canmove('left'):
self.coords[0] -= 1
我知道 canmove()
函数还不能正常工作,但它不会影响结果。
当 运行 Actor.move('up')
它减少 Actor.coords[1]
两个而不是一个。
下面是发生的情况(即使忽略 canmove()
检查):
>>> from actor import Actor
>>> actor = Actor([2, 2], [10, 5], [[4, 4]])
>>> actor.move('down')
>>> actor.coords
[2, 4]
和 actor.move(down)
应该将 actor.coords[1]
的值增加一,而不是两个。
在您的 canmove
函数 moving = self.coords
does not actually copy the list, it just copies a reference to where the list is located in memory (here is an explanation for what is going on 中有一个奇怪的细微差别。所以 moving
和 self.coords
都指向同一个列表,这导致你的 canmove
函数移动你的角色(然后当 move
函数移动它得到的角色移动了两次)。你想要做的是:
import copy
moving = copy.deepcopy(self.coords)
这将复制列表和列表中的所有项目。
我在 class 中有这个函数 move()
函数,它在包含 self.coords[1] += 1
这是文件:
class Actor():
def __init__(self, start, limits, barriers):
self.coords = start
self.limits = limits
self.barriers = barriers
def canmove(self, direction):
moving = self.coords
if(direction == 'up'):
moving[1] -= 1
elif(direction == 'right'):
moving[0] += 1
elif(direction == 'down'):
moving[1] += 1
elif(direction == 'left'):
moving[0] -= 1
if((moving[0] > self.limits[0]) or (moving[1] > self.limits[1]) or (-1 in moving) or (moving in self.barriers)):
return False
else:
return True
def move(self, direction):
if(direction == 'up'):
if self.canmove('up'):
self.coords[1] -= 1
elif(direction == 'right'):
if self.canmove('right'):
self.coords[0] += 1
elif(direction == 'down'):
if self.canmove('down'):
self.coords[1] += 1
elif(direction == 'left'):
if self.canmove('left'):
self.coords[0] -= 1
我知道 canmove()
函数还不能正常工作,但它不会影响结果。
当 运行 Actor.move('up')
它减少 Actor.coords[1]
两个而不是一个。
下面是发生的情况(即使忽略 canmove()
检查):
>>> from actor import Actor
>>> actor = Actor([2, 2], [10, 5], [[4, 4]])
>>> actor.move('down')
>>> actor.coords
[2, 4]
和 actor.move(down)
应该将 actor.coords[1]
的值增加一,而不是两个。
在您的 canmove
函数 moving = self.coords
does not actually copy the list, it just copies a reference to where the list is located in memory (here is an explanation for what is going on 中有一个奇怪的细微差别。所以 moving
和 self.coords
都指向同一个列表,这导致你的 canmove
函数移动你的角色(然后当 move
函数移动它得到的角色移动了两次)。你想要做的是:
import copy
moving = copy.deepcopy(self.coords)
这将复制列表和列表中的所有项目。