-2

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

  • 3
    At some point one of your `.next` calls returns is None – OneCricketeer Apr 02 '21 at 14:36
  • `selfPatient` or `otherPatient` is None. – John Gordon Apr 02 '21 at 14:38
  • Your while condition only checks if *at least one* node is not None. Use `and` instead of `or` to check both. – MisterMiyagi Apr 02 '21 at 14:41
  • Please provide the expected [MRE - Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). Show where the intermediate results deviate from the ones you expect. We should be able to paste a single block of your code into file, run it, and reproduce your problem. This also lets us test any suggestions in your context. We expect you to at least look up the error message and try to trace the offending values within your program. – Prune Apr 02 '21 at 14:45

1 Answers1

1

This checks if either are non-None, so if one is, and not the other, the loop is entered

while (selfPatient or otherPatient)

If you want to access both elem attributes, then both need to be non-None, requiring you to change the or to and

Otherwise, you need separate logic before the if statements to handle when only one is None, then maybe break the loop so that the return will be reached. And still wouldn't hurt to wrap the rest of the existing logic in if selfPatient and otherPatient since you're using attributes from both

As for why one becomes None, that depends on your logic for iterating the linked lists

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Totally true. Then I should check whether selfPatient or otherPatient is None and complete the merge. Thanks to everyone – FrugalCoder Apr 02 '21 at 14:43