0

I receive error: Traceback (most recent call last): File "...", line 36, in root.data.append("L") AttributeError: 'Node' object has no attribute 'data'

But i clearly have a data property in my class?

class Node:
    def _init_(self):
        self.left = None
        self.right = None
        self.data = list()

def addPerson(root, person):
    if person < root.data[0]:
        if root.left == None:
            root.left = Node()
            root.left.data.append(person)
        else:
            addPerson(root.left,person)
    else:
        if person > root.data[0]:
            if root.right == None:
                root.right = Node()
                root.right.data.append(person)
            else:
                addPerson(root.right,person)
        else:
            root.data.append(person)

def printPerson(root):
    if root == None:
        return
    print(root.data)
    printPerson(root.left)
    printPerson(root.right)

root = Node()
root.data.append("L")

for i in range(0,6):
    addPerson(root, input("Add Person: "))

print("Left side of room A-K: ")
printPerson(root.left)
print("Right side of room L-Z: ")
printPerson(root.right)

1 Answer 1

1

The initialization method name is incorrect, it should be __init__. These magic methods are often referred to as "dunders" because they begin and end with double underscores. Change your Node class definition to this:

class Node:
    def __init__(self):
        self.left = None
        self.right = None
        self.data = list()

https://docs.python.org/3/reference/datamodel.html#special-method-names

Sign up to request clarification or add additional context in comments.

2 Comments

WOW! can't believe i didn't catch that....new to python. C++ and C# is my normal language but I am in a course in school that is using python. Thanks
@ZacharyDaniels, it's happened to the best of us. I use a font which renders a blank pixel between adjacent underscores in order to avoid mistaking one underscore for two.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.