Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I am trying to make the linked_lists in python for the first time but my append function is not working properly. This is the code:

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")

This is the error I get:

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:

Error:

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

I copied the basic structure and functions of the linked list directly off of the youtube video and notice no difference between my code and his code.

1 Answer

0 votes
by (36.8k points)
edited by

 You missed to pass the self parameter that is the reason you are getting error:

def append(self, Name, Contact_number, Type, Urgency):

    new_node = node(self,Name,Contact_number,Type,Urgency)

Use Data Science with Python Course to improve your technical knowledge.

Interested to learn more about Python? Come & join our Python course

Browse Categories

...