I'm trying to figure out a way to define my function for loaning a book within a class. This is what I have so far:

class Library:
    # the class constructor
    def __init__(self, books, patrons):
        self.books=books
        self.patrons=patrons
    def __str__(self):
        s="Patron("
        for patron in self.patrons:
            s+=str(patron) + ', '
        if len(self.patron) != 0:
            s= s[:-2]
        s+=']'
        for book in self.books:
            s+=str(book) + ', '
        if len(self.books) != 0:
            s= s[:-2]
        s+='])'
        return s

and I have my function:

    def loan_book(self, patron_id, book_id):
        existence= False
        for i in self.patrons:
            if i.patron_id==patron_id:
                return True
        raise LookupError
        for i in self.books:
            if i.book_id==book_id:
                return True 
        raise LookupError
        for i in patron:
            if i.book_id==book_id:
                raise DuplicateIdError()
            self.books.remove(book_id)
            patron.books.append(book_id)

I just want to make sure it is correct. any help?

What is "patrons"? It looks like a list of Patron class instances. If so, please include the Patron class code also. In loan_book() you only check the book_id if the patron is not found. If the patron is found you return before the book lookup.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.