所以我有两个类 - 一个是书库,它实现为一个双向链表,另一个类是一个书籍节点的DLL节点类,它是这样的:
class Book:
def __init__(self,name,author,dateadded,viewed = False, nextnode=None, prevnode=None):
self._name = name
self._author = author
self._dateadded = dateadded
self._viewed = viewed
self._next = nextnode
self._prev = prevnode
def __str__(self):
""" Return string representation of book """
outstr = ""
outstr += self._name + " /n"
outstr += self._author + "/n"
outstr += self._dateadded
return outstr
def read(self):
""" Simulates the book being read & return string representation of the book """
self._viewed = True
outstr = "CURRENTLY READING: "
outstr += self._name
return outstr
我的图书馆类处理书籍节点的序列,如下所示:
class BookLibrary:
""" A Doubly Linked List implementing a book library """
def __init__(self):
self.size = 0
self.head = None
self.tail = None
我遇到的问题是在我的add_book()
方法中 - 当我运行它时,我得到的错误是TypeError: add_book() takes 1 positional argument but 5 were given
。这是功能本身,我真的不知道我哪里出错了:(
def add_book(book):
""" Add a book to the library in decreasing order of dateadded """
n = Book(book)
if self.size == 0:
self.head = n
self.tail = n
current_book = n
self.size += 1
elif self.head._dateadded <= n._dateadded:
n._next = self.head
self.head._prev = n
n._prev = None
self.head = n
self.size += 1
elif self.tail._dateadded <= n._dateadded:
self.tail._prev._next = n
n._prev = self.tail._prev
n._next = self.tail
self.tail._prev = n
self.size += 1
elif self.size != 0:
iterator = self.head._next
while iterator._next is not None:
if n._dateadded >= iterator._dateadded:
n._prev = iterator._prev
iterator._prev._next = n
n._next = iterator
iterator._prev = n
self.size += 1
break
else:
iterator = iterator._next
return None
我真的很感谢任何帮助,谢谢你
这是我运行的代码:
book_lib = BookLibrary()
book_lib.add_book("Harry Potter", "JK ROWLING", 19990101, False)
答案 0 :(得分:2)
如果您将add_book()
的所有参数传递给class Book
,那么这就是您的问题。它只期望一个参数book
,但我假设你传递了class Book
期望的所有参数。
最好首先实例化图书对象,然后将它们直接传递到add_book
方法中。