Library
Background
Library is a popular place to get a book when you need one. You might use it to get school books, or just to borrow a book to read. But have you ever wondered how the registration app works? Or what happens when you borrow a book?
In a file called library.py, implement a program in Python where you would simulate that app, you should do this by rewriting the distribution code below. The program should have two classes: Book and Library.
Understanding
Each book has a title, author and a genre.
A book can be borrowed, and returned. Also you can display info about each book that would be structured as title by author.
In a Library you can add a book to its collection. Find a book by its title and author. Find all books that are available, borrowed or a specific genre.
Before You Begin
Execute cd by itself in your terminal window. You should find that your terminal window’s prompt resembles the below:
Next execute to make a folder calledlibrary in your codespace.
Then execute
to change the directories into that folder. You should now see your terminal prompt as library/ . You can now execute
to make a file calledlibrary.py` where you'll write your program.
Specification
Book Class
__init__should initialize a book giventitle,author,genre,is_availablewheretitle,author,genreare passing in when creating an instance, andis_availablerepresents if the book is available or not.borrowmethod should changeis_availableto false.return_bookmethod should changeis_availableto true.display_infomethod should return the string of the book astitle by author
Library Class
- __init__ should have an attribute to store all books called books.
- add_book method should append the book given to books.
- find_book method should find the book given title and author and return it, else should return None
- list_available_books method should list all available books, if there are none it should return an empty list.
- list_borrowed_books method should list all borrowed books, if there are none it should return an empty list.
- list_genre_books method should list all books with the given genre, if there are none it should return an empty list.
Rewrite the code into a Class version of it.
Hints
Class methods and instances https://docs.python.org/3/tutorial/classes.html#class-objects
Challenge
If up for a challenge, optionally implement a program that asks for user input to interact with your library. The program should support borrowing, returning, and searching for books using all of the methods described.
Remember to add at the if name main clause bottom of the file!
How To Test
Here’s how to test your code manually:
Open your library.py file, create two Book instances (you can use this books as input: "The night, The fool and The Dead by Steve Cole, Scifi", "The Name of the Wind by Patrick Rothfuss, Fantasy"). Create a library instance.
-
Use the
add_bookmethod to add at least one book to the library. Then print all of the books. You should see the books you added. -
Use the
find bookmethod to find a book you haven't added. It should returnNone. Then use it to find the book that is already there, and it should return the book's instance.
Use the borrow method on a book.
- Use the
list_borrowed_booksmethod, where you should see the book you have borrowed.
Use the return_book method on a book.
-
Use the
list_available_booksmethod to list all of the books you have added. You should see the books you have added. -
Use the
display_bookmethod on a book, which should returntitle by authorstring. -
Use the
list_genre_booksmethod with a genre that is not in any books, it should return an empty list. Then use the method again but with a genre that is in some books, it should return all of the books with that genre.
If you run into an error saying your file cannot be opened, retrace your steps to be sure that you are inside your library folder and have saved your library.py file there.
You can execute the below to check your code using check50, a program that CS50 will use to test your code:
- Green smiles mean your program has passed a test!
- Red frowns will indicate your program output something unexpected.
- Orange neutral faces mean you must fix the failed check before those checks can run.
Visit the URL that check50 outputs to see the input check50 handed to your program, what output it expected, and what output your program actually gave.
How to Submit
Coming soon