Question

from node import *
from copy import deepcopy
import pygame
from PIL import Image

def buscar_solucion(inicio, solucion):

    visitados=[]
    nodo_i=Nodo(inicio)

    while nodo_i.get_datos() != solucion:

        nodo_i = deep_search(nodo_i,solucion,visitados)

    moves=[]
    moves.append(solucion)

    while nodo_i.get_padre() != None:
        pater=nodo_i.get_padre()
        moves.append(pater.get_datos())
        nodo_i=pater
    moves.reverse()
    print moves

 def operaciones_h(dades):
   r=[]
   i=0
   while dades[i] != dades[-1]:
      s=deepcopy(dades)
      s[i]=dades[i+1]
      s[i+1]=dades[i]
      r.append(Nodo(s))
      i+=1
   return r

def deep_search(ini,sol,visit):

    visit.append(ini.get_datos())

    if ini.get_datos() == sol:
        return ini

    else:
        hijos=operaciones_h(ini.get_datos())
        for hijo in hijos:
            if hijo.get_datos() not in visit:
                hijo.set_padre(ini)
                return deep_search(hijo,sol,visit)

The problem I am getting is in line 14, when creating an instance of the Class Nodo. The error I get is:

AttributeError: 'NoneType' object has no attribute 'get_datos'

The Class is defined in a file called node.py. Might the problem be in the import? Or when defining the instance?

Was it helpful?

Solution

The problem is in your deep_search function - it doesn't always return anything. Consider for example what happens if operaciones_h(ini.get_datos()) returns an empty list: the for hijo in hijos loop will never be entered. And even then, if all the datos are in visit, the if statement will never be True and you will never hit the return statement. In both of those cases, the value of deep_search will be None.

You should ensure that all paths through deep_search end with returning a Nodo object.

OTHER TIPS

The nodo_i = deep_search(nodo_i,solucion,visitados) call returns None for some cases. Inside deep_search you have the following code:

else:
    hijos=operaciones_h(ini.get_datos())
    for hijo in hijos:
        if hijo.get_datos() not in visit:

If operaciones_h returns an empty list or if none of the hijo values matches your if statement the function returns None and you'll see your exception.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top