Почему я получаю AttributeError: у объекта 'NoneType' нет атрибута 'value'? - PullRequest
0 голосов
/ 09 октября 2019

Ниже приведен мой код на 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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...