为什么会出现AttributeError:'NoneType'对象没有属性'value'?

时间:2019-10-09 19:07:59

标签: python linked-list

以下是我在python中的代码。我基本上是在尝试查看链接列表,以查看该列表中是否有特定元素。如果项目在列表中,则该代码有效,如果不在列表中,则该代码无效。我相信问题出在循环中,直到列表的末尾和最后一个节点,但是我不确定如何解决该错误。任何帮助,将不胜感激。 (已编辑以包括所使用的类)


class Node:

    def __init__(self, data):

        #raise error for wrong data input type
        if not type(data) in [int, float, str]:
            raise TypeError("data must be an integer, float, or string")

        self.value = data


class LinkedListNode(Node):

    def __init__(self, data):



        Node.__init__(self, data)       # Use inheritance to set self.value.
        self.next = None                # Reference to the next node.
        self.prev = None                # Reference to the previous node.



class LinkedList:

    def __init__(self):

        self.head = None
        self.tail = None
        self.length = 0

    def append(self, data):

        # Create a new node to store the input data.
        new_node = LinkedListNode(data)
        if self.head is None:
            # If the list is empty, assign the head and tail attributes to
            # new_node, since it becomes the first and last node in the list.
            self.head = new_node
            self.tail = new_node
            self.length += 1
        else:
            # If the list is not empty, place new_node after the tail.
            self.tail.next = new_node               # tail --> new_node
            new_node.prev = self.tail               # tail <-- new_node
            # Now the last node in the list is new_node, so reassign the tail.
            self.tail = new_node
            self.length += 1


    def find(self, data):

        x = range(self.length)
        current_position = self.head.value
        current_node = self.head

        for i in x:
            if current_position == data:
                return self.head
            else:
                current_node = current_node.next
                current_position = current_node.value


        if current_position != data:
            raise ValueError("data point is not in the list")

        return

0 个答案:

没有答案