Jan-16-2018, 03:51 PM
Hi, I am trying to store some information with regards to books into an object. My object class codes are as below:
class Book: def __init__(self,Title,Author,BookID): self.Title=Title self.Author=Author self.BookID=BookID def __repr__(self): return "Book()" def __str__(self,Title,Author,BookID): return Title,Author,BookIDMy Other Codes Are
class Node: def __init__(self, data): self.data = data self.next = None class Book: def __init__(self,Title,Author,BookID): self.Title=Title self.Author=Author self.BookID=BookID def __repr__(self): return "Book()" def __str__(self,Title,Author,BookID): return Title,Author,BookID class LinkedList: def __init__(self): self.head = None self.size=0 # Defining the method to print the linked list def DisplayBook(self): temp = self.head while temp: print (temp.data) temp = temp.next def AddBookToFront(self, data): #AddBookToFront new_node = Node(data) # 3. Make next of new Node as head new_node.next = self.head # 4. Move the head to point to new Node self.head = new_node self.size+=1 def RemoveBookAtPosition(self,position): # If linked list is empty if self.head == None: return # Store head node temp = self.head # If head needs to be removed if position == 0: head = temp.next temp = None return # Find previous node of the node to be deleted for i in range(position -1 ): temp = temp.next if temp is None: break # If position is more than number of nodes if temp is None: return if temp.next is None: return # Node temp.next is the node to be deleted # store pointer to the next of node to be deleted next = temp.next.next # Unlink the node from linked list temp.next = None temp.next = next def AddBookAtPosition(self,data,position): if(position==0): newnode=Node(data) newnode.next=self.head self.head=newnode elif(position>self.size): print("\n\nIndex is out of range\n\n") elif(position==self.size): self.AddBookToFront(data) else: current=self.head count=0 while(current!=None): if(count==position-2): break else: count+=1 current=current.next newnode=Node(data) newnode.next=current.next current.next=newnode def AddBookAtPosition(self,data,position): if(position==0): newnode=Node(data) newnode.next=self.head self.head=newnode elif(position>self.size): print("\n\nIndex is out of range\n\n") elif(position==self.size): self.AddBookToFront(data) else: current=self.head count=0 while(current!=None): if(count==position-2): break else: count+=1 current=current.next newnode=Node(data) newnode.next=current.next current.next=newnode # Defining function which will merge two linked lists def mergeLists(l1, l2): temp = None if l1 is None: return l2 if l2 is None: return l1 if l1.data <= l2.data: temp = l1 temp.next = mergeLists(l1.next, l2) else: temp = l2 temp.next = mergeLists(l1, l2.next) return temp # Defining function which will sort the linked list using mergeSort def mergeSort(head): if head is None or head.next is None: return head l1, l2 = divideLists(head) l1 = mergeSort(l1) l2 = mergeSort(l2) head = mergeLists(l1, l2) return head # Defining function which will divide a linked list into two equal linked lists def divideLists(head): slow = head # slow is a pointer to reach the mid of linked list fast = head # fast is a pointer to reach the end of the linked list if fast: fast = fast.next while fast: fast = fast.next # fast is incremented twice while slow is incremented once per loop if fast: fast = fast.next slow = slow.next mid = slow.next slow.next = None return head, midThe Error Output That I Am Getting is as below:
Error:Traceback (most recent call last):
File "C:\Users\TP_baseline\Desktop\DSAG\ProjectTest8-2.py", line 143, in <module>
ll.DisplayBook()
File "C:\Users\TP_baseline\Desktop\DSAG\ProjectTest8-2.py", line 25, in DisplayBook
print (temp.data)
TypeError: __str__() missing 3 required positional arguments: 'Title', 'Author', and 'BookID'
Any guidance on solving this error would be greatly appreciated.