i = self._randbelow(len(seq)) TypeError: object of type 'NoneType' has no len()
Question: I have this error when I'm running my code for building a perfect maze. Here
is the code:

    
    
       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])
    

Here is the Error I got:

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

I think 'neighboor' is a list contains four sets of number, it should have
len().

I'm new to programming, any help would be appreciate.


Answer: Couple of things. First, if you're using something like choice() that's
imported from a wildcard import (from random import *), it would be useful to
include that, because otherwise we're just guessing where you got that
function from. Also, [wildcard imports are considered bad practice
anyway](http://stackoverflow.com/questions/3615125/should-wildcard-import-be-
avoided).

The problem is that the remove() method of a list returns None. You cannot
choose something from None, because None is not an iterable. That is to say
that the remove() method does not support chaining. Try changing:

    
    
    if len(neighboor) != 0: new = choice(neighboor.remove(new)) # Passes None to choice() which raises an error
    

to

    
    
    if len(neighboor) != 0:
        neighboor.remove(new) # removes the element from neighboor
        new = choice(neighboor) # Chooses from the list as you intended
    

You could have other errors, but that's the one that came from the traceback
you posted.

For the future, I recommend familiarizing yourself with Python's traceback,
because it is telling you exactly what is going wrong. [Here's a
resource](http://cbio.ufs.ac.za/live_docs/nbn_tut/understanding_errors.html).

As an aside, do you mean neighbor instead of neighboor?

