i = self._randbelow(len(seq)) TypeError: object of type 'NoneType' has no len()
i = self._randbelow(len(seq)) TypeError: object of type 'NoneType' has no len()
我在 运行 构建完美迷宫的代码时出现此错误。
这是代码:
def walk(self, s, x, y):
neighboor = [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]
if s.size() == self.size**2: return
else:
while True:
new = choice(neighboor)
if self.is_valid(new[0], new[1]): break
while self.maze[new[0]][new[1]].getVisit():
if len(neighboor) != 0: new = choice(neighboor.remove(new))
else:
temp = s.pop(s)
self.walk(s, temp[0], temp[1])
#print(new)
self.maze[new[0]][new[1]].changeVisit()
if new == neighboor[1]:
self.maze[x][y].changeNorth()
self.maze[new[0]][new[1]].changeSouth()
elif new == neighboor[0]:
self.maze[x][y].changeSouth()
self.maze[new[0]][new[1]].changeNorth()
elif new == neighboor[2]:
self.maze[x][y].changeEast()
self.maze[new[0]][new[1]].changeWest()
elif new == neighboor[3]:
self.maze[x][y].changeWest()
self.maze[new[0]][new[1]].changeEast()
s.push(new)
self.walk(s, new[0], new[1])
这是我得到的错误:
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
a.search()
File "C:\Users\Serena\Desktop\XAproject\Stack.py", line 93, in search
self.walk(s, startX, startY)
File "C:\Users\Serena\Desktop\XAproject\Stack.py", line 76, in walk
self.walk(s, new[0], new[1])
File "C:\Users\Serena\Desktop\XAproject\Stack.py", line 76, in walk
self.walk(s, new[0], new[1])
File "C:\Users\Serena\Desktop\XAproject\Stack.py", line 76, in walk
self.walk(s, new[0], new[1])
File "C:\Users\Serena\Desktop\XAproject\Stack.py", line 76, in walk
self.walk(s, new[0], new[1])
File "C:\Users\Serena\Desktop\XAproject\Stack.py", line 76, in walk
self.walk(s, new[0], new[1])
File "C:\Users\Serena\Desktop\XAproject\Stack.py", line 76, in walk
self.walk(s, new[0], new[1])
File "C:\Users\Serena\Desktop\XAproject\Stack.py", line 52, in walk
if len(neighboor) != 0: new = choice(neighboor.remove(new))
File "C:\Python34\lib\random.py", line 253, in choice
i = self._randbelow(len(seq))
TypeError: object of type 'NoneType' has no len()
我认为'neighboor'是一个包含四组数字的列表,应该有len()。
我是编程新手,如有任何帮助,我们将不胜感激。
几件事。首先,如果您使用的是从通配符导入(来自随机导入 *)导入的 choice() 之类的东西,那么包含它会很有用,否则我们只是猜测您从哪里获得该函数。另外,wildcard imports are considered bad practice anyway.
问题是列表的 remove() 方法 returns None。您不能从 None 中选择某些内容,因为 None 不是可迭代的。也就是说 remove() 方法不支持链接。
尝试更改:
if len(neighboor) != 0: new = choice(neighboor.remove(new)) # Passes None to choice() which raises an error
至
if len(neighboor) != 0:
neighboor.remove(new) # removes the element from neighboor
new = choice(neighboor) # Chooses from the list as you intended
您可能还有其他错误,但这是您发布的回溯错误。
为了将来,我建议您熟悉 Python 的回溯,因为它会准确地告诉您出了什么问题。 Here's a resource.
顺便说一句,你是指邻居而不是邻居吗?
我在 运行 构建完美迷宫的代码时出现此错误。 这是代码:
def walk(self, s, x, y):
neighboor = [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]
if s.size() == self.size**2: return
else:
while True:
new = choice(neighboor)
if self.is_valid(new[0], new[1]): break
while self.maze[new[0]][new[1]].getVisit():
if len(neighboor) != 0: new = choice(neighboor.remove(new))
else:
temp = s.pop(s)
self.walk(s, temp[0], temp[1])
#print(new)
self.maze[new[0]][new[1]].changeVisit()
if new == neighboor[1]:
self.maze[x][y].changeNorth()
self.maze[new[0]][new[1]].changeSouth()
elif new == neighboor[0]:
self.maze[x][y].changeSouth()
self.maze[new[0]][new[1]].changeNorth()
elif new == neighboor[2]:
self.maze[x][y].changeEast()
self.maze[new[0]][new[1]].changeWest()
elif new == neighboor[3]:
self.maze[x][y].changeWest()
self.maze[new[0]][new[1]].changeEast()
s.push(new)
self.walk(s, new[0], new[1])
这是我得到的错误:
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
a.search()
File "C:\Users\Serena\Desktop\XAproject\Stack.py", line 93, in search
self.walk(s, startX, startY)
File "C:\Users\Serena\Desktop\XAproject\Stack.py", line 76, in walk
self.walk(s, new[0], new[1])
File "C:\Users\Serena\Desktop\XAproject\Stack.py", line 76, in walk
self.walk(s, new[0], new[1])
File "C:\Users\Serena\Desktop\XAproject\Stack.py", line 76, in walk
self.walk(s, new[0], new[1])
File "C:\Users\Serena\Desktop\XAproject\Stack.py", line 76, in walk
self.walk(s, new[0], new[1])
File "C:\Users\Serena\Desktop\XAproject\Stack.py", line 76, in walk
self.walk(s, new[0], new[1])
File "C:\Users\Serena\Desktop\XAproject\Stack.py", line 76, in walk
self.walk(s, new[0], new[1])
File "C:\Users\Serena\Desktop\XAproject\Stack.py", line 52, in walk
if len(neighboor) != 0: new = choice(neighboor.remove(new))
File "C:\Python34\lib\random.py", line 253, in choice
i = self._randbelow(len(seq))
TypeError: object of type 'NoneType' has no len()
我认为'neighboor'是一个包含四组数字的列表,应该有len()。
我是编程新手,如有任何帮助,我们将不胜感激。
几件事。首先,如果您使用的是从通配符导入(来自随机导入 *)导入的 choice() 之类的东西,那么包含它会很有用,否则我们只是猜测您从哪里获得该函数。另外,wildcard imports are considered bad practice anyway.
问题是列表的 remove() 方法 returns None。您不能从 None 中选择某些内容,因为 None 不是可迭代的。也就是说 remove() 方法不支持链接。 尝试更改:
if len(neighboor) != 0: new = choice(neighboor.remove(new)) # Passes None to choice() which raises an error
至
if len(neighboor) != 0:
neighboor.remove(new) # removes the element from neighboor
new = choice(neighboor) # Chooses from the list as you intended
您可能还有其他错误,但这是您发布的回溯错误。
为了将来,我建议您熟悉 Python 的回溯,因为它会准确地告诉您出了什么问题。 Here's a resource.
顺便说一句,你是指邻居而不是邻居吗?