如何在此Python代码中解决此错误 [英] How do I address this error in this Python code

查看:82
本文介绍了如何在此Python代码中解决此错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用Python编写程序作为赋值的一部分,我需要使用LinkedList和Nodes来解决问题。但是当我尝试运行程序时,我收到一条错误消息:



I am trying to write a program in Python as part of an assignment and I needed to use LinkedList and Nodes To Solve The Question. However when I try to run the program I am getting an error message as such:

Traceback (most recent call last):
  File "C:\Users\baseline\Desktop\DSAG\ProjectTest2.py", line 75, in <module>
    ll.AddBookAtPosition(book5, 3)
  File "C:\Users\baseline\Desktop\DSAG\ProjectTest2.py", line 49, in AddBookAtPosition
    head=head.next
AttributeError: 'function' object has no attribute 'next'





任何帮助或指导我的建议正确的方向将受到极大的赞赏



我的尝试:





Any Help Or Advice To Guide Me In The Right Direction Would Be Greatly Appreciated

What I have tried:

This Is My Code That I have written so far

<pre>class Node :
    def __init__(self, newData):
        self.data = newData
        self.next  = None
    
    def getData(self):
        return self.data
    
    def setData(self,newData):
        self.data = newData

    def getNext(self):
        return self.next

    def next(self):
        return self.next

    def setNext(self,newNode):
        self.next = newNode


class LinkedList :
    def __init__(self):
        self.head  = None

    def AddBookToFront(self,newNode):
        newNode.setNext(self.head)
        self.head = newNode

    def next(self):
        return self.next

    def insertAfter(self, prev_node, new_data):
            
            if prev_node is None:
                return

            new_node = Node(new_data)
     
            new_node.next = prev_node.next
     
            prev_node.next = new_node

    def AddBookAtPosition(head, newNode, position):
        start=head
        if position == 0:
            return Node(newNode, head)
        while position > 1:
            head=head.next
            position-=1
        head.next= Node(newNode, head.next)
        return start     
    
    def printAll(self):
        cursor = self.head
        while cursor is not None:
            print(cursor.getData())
            cursor=cursor.getNext()

book1=("abc", "Peter")
book2=("def", "James")
book3=("ghi", "Sam")
book4=("jkl", "Tom")
book5=("mno", "Jessie")

a = Node(book1)
b = Node(book2)
c = Node(book3)
d = Node(book4)

ll=LinkedList()
ll.AddBookToFront(b)
ll.AddBookToFront(a)
ll.insertAfter(ll.head.next, book4)
ll.AddBookAtPosition(book5, 3)

ll.printAll()

推荐答案

您的类的属性 next ,函数 next 。这不是一个好主意,因为正如您所看到的,碰撞发生了。重命名函数或属性。
Your class both has an attribute next and a function next. This is not a good idea because, as you can see, collisions happen. Rename either the function or the attribute.


这篇关于如何在此Python代码中解决此错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆