Python 3-__init __()接受1到5个位置参数,但给出了6个

时间:2020-10-14 04:55:35

标签: python python-3.x

我第一次尝试在python中创建链表,但是我的append函数似乎无法正常工作。到目前为止,这是代码:

class node:
    def __init__(self, Name = None, Contact_number = None, Type = None, Urgency = None):
        self.Name = Name
        self.Contact_number = Contact_number
        self.Type = Type
        self.Urgency = Urgency
        self.next = None

class linked_list:
    def __init__(self):
        self.head = node()
    
    def append(self, Name, Contact_number, Type, Urgency):
        new_node = node(self,Name,Contact_number,Type,Urgency)
        cur = self.head
        while cur.next != None:
            cur = cur.next
        cur.next = new_node
    
    def length(self):
        cur = self.head()
        total = 0
        while cur.next != None:
            total+=1
            cur=cur.next
        return total
    
    def display(self):
        Appointments = []
        cur_node = self.head
        while cur_node.next != None:
            cur_node = cur_node.next
            aptmnt = (cur_node.Name, cur_node.Contact_number, cur_node.Type, cur_node.Urgency)
            Appointments.append(aptmnt)
        print(Appointments)
    
    
general_surgeon = linked_list()
general_surgeon.display()

general_surgeon.append("Akash", "827xxxxxx1", "Min Surgery", "no")

这是尝试使用append函数时遇到的错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-27-0ffb4f93a9c0> in <module>
     39 general_surgeon.display()
     40 
---> 41 general_surgeon.append("Akash", "827xxxxxx1", "Min Surgery", "no")

<ipython-input-27-0ffb4f93a9c0> in append(self, Name, Contact_number, Type, Urgency)
     12 
     13     def append(self, Name, Contact_number, Type, Urgency):
---> 14         new_node = node(self,Name,Contact_number,Type,Urgency)
     15         cur = self.head
     16         while cur.next != None:

TypeError: __init__() takes from 1 to 5 positional arguments but 6 were given

我一直在直接从youtube视频中复制链接列表的基本结构和功能,除了我的数据变量更多之外,我的代码和他的代码之间没有区别。

请帮助?

2 个答案:

答案 0 :(得分:1)

def append(self, Name, Contact_number, Type, Urgency):
    new_node = node(self,Name,Contact_number,Type,Urgency)

由于您正在调用node类,因此不需要传递self参数。

答案 1 :(得分:1)

这是错误,已经显示为TypeError

TypeError: __init__() takes from 1 to 5 positional arguments but 6 were given

因此,您要提供6个参数,而不是line # 14

中的5个参数

无需在self的{​​{1}}中将node class作为参数传递

尝试一下:

line # 14