noneType object is not iterable in Python

Issue

This Content is from Stack Overflow. Question asked by Weredoge

I’m trying to make a pathfinding game using A* algorithm, then this error keeps appearing no matter what i do. it’s said TypeError: ‘NoneType’ object is not iterable at “for m in self.get_neighbors(n):”. Sorry for any spelling mistake, English is not my native language.

def astar(self,start,end):
    open_set = set(start)
    closed_set = set()
    g = {}               #store distance from starting node
    parents = {}         # parents contains an adjacency map of all nodes
    #distance of starting node from itself is zero
    g[start] = 0
    #start_node is root node i.e it has no parent nodes
    #so start_node is set to its own parent node
    parents[start] = start
    while len(open_set) > 0:
        n = ()
        #node with lowest f() is found
        for v in open_set:
            if len(n)<=0 or g[v] + self.heuristic(v,end) < g[n] + self.heuristic(n,end):
                n = v
        if n == end or self.check_move(n) == False:
            pass
        else:
            for m in self.get_neighbors(n):
                #nodes 'm' not in first and last set are added to first
                #n is set its parent
                if m not in open_set and m not in closed_set:
                    open_set.add(m)
                    parents[m] = n
                    g[m] = g[n] + 1
                #for each node m,compare its distance from start i.e g(m) to the
                #from start through n node
                else:
                    if g[m] > g[n] + 1:
                        #update g(m)
                        g[m] = g[n] + 1
                        #change parent of m to n
                        parents[m] = n
                        #if m in closed set,remove and add to open
                        if m in closed_set:
                            closed_set.remove(m)
                            open_set.add(m)
        if len(n)<=0:
            print('Path does not exist!')
            return None
    
        # if the current node is the stop_node
        # then we begin reconstructin the path from it to the start_node
        if n == end:
            path = []
            while parents[n] != n:
                path.append(n)
                n = parents[n]
            path.append(start)
            path.reverse()
            print('Path found')
            return path
        # remove n from the open_list, and add it to closed_list
        # because all of his neighbors were inspected
        open_set.remove(n)
        closed_set.add(n)
    print('Path does not exist!')
    return None    

def move_to_point(self,point):
    start = self.get_start()
    end = point
    
    print(start,point)
    
    return None



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?