Answer to Question #322971 in Python for DANIE

Question #322971

1.      Create a library of books in Python, with the following attributes.

(i) Title of book

(ii) Author of book

(iii) ISBN (International Standard Book Number)

(iv) Year of Publication.

(v) Book Classification (e.g. Fiction, Science, Art)



1
Expert's answer
2022-04-06T07:10:42-0400
class Book:
    def __init__(self, title, author, isbn=None, year=None, rubric=None):
        self._title = title
        self._author = author
        self._isbn = isbn
        self._year = year
        self._class = rubric



    def __str__(self):
        res = "Title: " + self._title
        res += "\nAuthor: " + self._author
        if self._isbn:
            res += "\nISBN: " + str(self._isbn)
        if self._year:
            res += "\nYear: " + str(self._year)
        if self._class:
            res += "\nClass: " + self._class
        res += '\n'
        return res


def main():
    library = []
    library.append(Book("Harry Potter and the Sorcerer's Stone", 
                        "J.K.Rowling", year=1998, rubric="fantasy"))
    library.append(Book("The Art of Computer Programming, vol.1", "Donald E.Knuth", 
                        9780201896831, 1998, "scince"))
    library.append(Book("The Louvre: All the Paintings Paperback", "Erich Lessing",
                        year=2020, rubric="art"))
    library.append(Book("The Ultimate Hitchhiker's Guide", "Douglas Adams",
                        year=1996))
    
    for book in library:
        print(book)


if __name__ == '__main__':
    main()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS