Good morning, I really do not have so much experience programming in Python (I'm a student, really) what happens is the following: I have an assignment in which I must put into practice the use of linked lists, creating nodes.
And then that, I create the nodes with a class defined for them and I add values to them, but at the time of printing them I get the following error:
Traceback (Most recent call last): File "class.py", line 62, in print (c.getInfo ()) File "class.py", line 24, in getInfo return str (info.getTitle ()) NameError: Global Name 'info' is not defined
The code is as follows:
class Nodo():
def __init__(self,pos):
siguiente = ''
self.pos = pos
elemento = ''
info = NodoInfo(self.pos)
def getSiguiente(self):
return self.siguiente
def setSiguiente(self,n):
self.siguiente = n
def getPos(self):
return pos
def setElemento(self,a):
elemento = a
def getElemento(self):
return elemento
def getInfo(self):
return str(info.getTitulo())
class NodoInfo():
def __init__(self,pos):
self.titulo = "Numero de nodo: "+str(pos)
self.desc = "Este nodo puede almacenar datos."
self.detalle = "Especificamente, almacena datos de tipo Object (Amplio espectro de TDA's)."
def getTitulo(self):
return self.titulo
def getDesc(self):
return self.desc
def getDetalle(self):
return self.detalle
pos = 1
seguir = 1
n = Nodo(pos)
c = Nodo(pos)
x = raw_input("Ingrese su primer dato: ")
n.setElemento(x)
while(seguir):
pos = pos + 1
opc = raw_input("Desea ingresar otro dato? S/N ")
if(opc == 'S'):
a = Nodo(pos)
n.setSiguiente(a)
a.setElemento(raw_input("Ingrese el otro dato: "))
if(c.getSiguiente==''):
c=n
n=a
else:
seguir = 0
for i in range (1,pos):
print(c.getInfo())
print("El nodo almacena: "+c.getElemento())
I would appreciate your help! =)