i = self._randbelow(len(seq))TypeError:类型对象' NoneType'没有len()

时间:2015-04-05 04:24:17

标签: python typeerror

当我运行我的代码来构建一个完美的迷宫时,我遇到了这个错误。 这是代码:

   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()。

我是编程新手,任何帮助都会很感激。

1 个答案:

答案 0 :(得分:4)

一些事情。首先,如果您正在使用从通配符导入(从随机导入*)导入的choice()之类的内容,那么包含它会很有用,因为否则我们只是猜测您从哪里获得该函数。另外,wildcard imports are considered bad practice anyway

问题是列表的remove()方法返回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

顺便说一句,你的意思是邻居而不是邻居吗?