0

so i have two classes - one is a book library that is implemented as a doubly linked list and the other class is a DLL node class for a book node which is this:

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

my book library class which deals with a sequence of the book nodes, looks like this:

class BookLibrary:

""" A Doubly Linked List implementing a book library """

def __init__(self):
    self.size = 0
    self.head = None
    self.tail = None

the problem im having is within my add_book() method - when i run it the error im getting is TypeError: add_book() takes 1 positional argument but 5 were given . This is the function itself, I dont really know where im going wrong :(

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

i would really appreciate any help thank you

this is the code i run:

book_lib = BookLibrary()
book_lib.add_book("Harry Potter", "JK ROWLING", 19990101, False)
0

1 Answer 1

2

If you are passing in all of the arguments to add_book() that you would to the class Book, this is your problem. It is only expecting one argument, book, but I assume you are passing in all of the arguments that class Book expects.

You are better off instantiating your book objects first and then passing them directly into your add_book method.

Sign up to request clarification or add additional context in comments.

4 Comments

i see what you're saying but is not instantiating the book object in the line : n = Book(book) in the add_book() method already
no you need to instantiate it in your main code , ideally you would create a book and than add it to the Library. but if you want to add it dynamically you can do book_lib.add_book(Book("Harry Potter", "JK ROWLING", 19990101, False))
also, if you want your code to run you are going to have to add self as the first parameter to add_book def add_book(self, book):
and the last thing you need to do is change all of your ns in the add_book function to book

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.