Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is some code that should get you well on your way:</p> <pre><code>import sys # Python 2/3 compatibility shim if sys.hexversion &gt;= 0x3000000: inp = input rng = range else: inp = raw_input rng = xrange def get_int(prompt): while True: try: return int(inp(prompt)) except ValueError: pass class Book: @classmethod def from_prompt(cls): title = inp('Enter title: ').strip() author = inp('Enter author: ').strip() pages = get_int('Page count: ') date = inp('Publication date: ').strip() return cls(title, author, pages, date) def __init__(self, title, author, pages, date): self.title = title self.author = author self.pages = pages self.date = date def __str__(self): return "Book: {title}\nAuthor: {author}\nPage count: {pages}\nPublish Date: {date}".format( title=self.title, author=self.author, pages=self.pages, date=self.date ) class Library: def __init__(self, books=None): self.books = [] self.title_index = {} if books: for book in books: self.add(book) def add(self, book): self.books.append(book) self.title_index[book.title] = book def find_title(self, title): return self.title_index.get(title, None) def __str__(self): return '\n\n'.join(str(book) for book in self.books) def main(): # # You get to write this part, # but here are some code examples: # lib = Library() book1 = Book('Fun with Dick and Jane', 'May Hill Arbuthnot', 160, '1951') lib.add(book1) book2 = Book('The Adventures of Pinocchio', 'Carlo Collodi', 171, '1911') lib.add(book2) # prompt for book 3 information book3 = Book.from_prompt() lib.add(book3) # find a book and display its information print('='*30) print(lib.find_title('The Adventures of Pinocchio')) # display the whole library contents print('='*30) print(lib) if __name__=="__main__": main() </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload