Question: class Book(object): A Book Object. def __init__(self, title, author, pub_yr='Unknown'): self.title = title self.author = author self.pub_yr = pub_yr def __repr__(self): return ' ' %
class Book(object): """A Book Object.""" def __init__(self, title, author, pub_yr='Unknown'): self.title = title self.author = author self.pub_yr = pub_yr
def __repr__(self): return '
class Library(object): """A collection of book objects."""
def __init__(self): self.books = []
def __repr__(self): return '
def list_books(self): """List all the books in the library."""
if len(self.books) == 0: print "Library is empty." return for book in self.books: print book.title print ' Author:', book.author print ' Published:', book.pub_yr print
def add_book(self, book): """Add a book to the library."""
if not isinstance(book, Book): raise Exception('Please enter a Book Object to add to the library') self.books.append(book) print 'Added: %s' % (book.title)
def empty_library(self): """Remove all books from the library.""" pass
########### WRITE YOUR SCRIPT HERE # Details on what the script should include are in the skills assessment # instructions. b1 =Book("The Bell Jar", "Sylvia Plath", 1963) b2 =Book("Anna Karenina", "Leo Tolstoy")
l = library ()
Question:
Add the two books you created to the library (the method for adding books to the library is already created.)
Call the method that lists all the books in the library again. It should now display the two books.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
