I was working with doubly linked lists in Python when I encountered with this error:
def merge(self,other):
mergedCenter = HealthCenter()
selfPatient = self._head
otherPatient = other._head
print(selfPatient.elem)
print(selfPatient.elem < otherPatient.elem)
while (selfPatient or otherPatient):
if selfPatient.elem < otherPatient.elem:
mergedCenter.addLast(selfPatient.elem)
selfPatient = selfPatient.next
elif selfPatient.elem > otherPatient.elem:
mergedCenter.addLast(otherPatient.elem)
otherPatient = otherPatient.next
else:
mergedCenter.addLast(selfPatient.elem)
selfPatient = selfPatient.next
otherPatient = otherPatient.next
return (mergedCenter)
I get this output:
Abad, Ana 1949 True 0
True
Traceback (most recent call last):
File "main.py", line 189, in <module>
hc3 = hc1.merge(hc2)
File "main.py", line 112, in merge
if selfPatient.elem < otherPatient.elem:
AttributeError: 'NoneType' object has no attribute 'elem'
There's already a method implemented to compare .elem as you can clearly see in the second print, but I don't understand why in the first conditional it keeps breaking.
Thanks in advance.
Solution: Had to take into account the length of both DLists, not to get one of both values 'Nonetype'. This is solved changing the OR for AND, and checking later which one is not 'Nonetype' to finish merging