Lab - Little Free Library
This activity will help you practice some of the concepts from this week and prepare for the homework problems. Specifically, you will practice:
- Creating lists to store sequences of objects
- Using methods to add/remove items from lists
- Searching for items and counting occurrences in a list
Style:
Make sure you include a docstring at the top of your file:
""" Lab 3 - Description.
Author: YOUR NAME
Date: Due Date
Honor Code:
"""
See below for example:
""" Lab 3 – Little Free Library.
Author: Alvin Chao
Date: 9/9/22
Honor Code: I worked with Pongo. (or, This is my own work.)
"""
Helpful Resources:
Book section 3.2: List basics – You will need to use these methods! Ex: the_name_of_my_list.append('hi')
Instructions:
A “Little Free Library” is a community library that lets people borrow books and contribute to the library. The library survives on the “take a book, leave a book” principle. If someone comes and takes the book “Project Hail Mary”, they have to add a book to the library (like “The Martian”). It’s a great way for people to share books!
In this lab, you will create a program to simulate a Little Free Library. You, being a superstar Python coder, realize that a row of books is just a sequence. You remember from class that the list datatype can be used to represent sequences. You crack your knuckles and get ready to code this thing!
A few guidelines for the program:
- Your file must be named library.py
- At the top of your code, after the docstring, you must include a variable named books that contains the list of books in the library:
books = ['Project Hail Mary', 'The Hobbit', 'Sherlock Holmes', 'The Hobbit', 'Dune']
This will magically be filled with different books by Gradescope when running your program.
- The first thing the program should do is print out a cool title, the number of books in the library, and then all the books, followed by a blank line:
= Little Free Library = Count: 5 book(s) ['Project Hail Mary', 'The Hobbit', 'Sherlock Holmes', 'The Hobbit', 'Dune']
- The program should then ask the user which book they are interested in, and then tell the user how many copies of that book are available:
Enter the title of a book to find: The Hobbit We have 2 copies of "The Hobbit" available.
Hint: Look at the list of methods in Helpful Resources to see if you can find a way to count something in a list.
- Then, the program should ask the user for the index of a book to take, print the name of the book, then remove it from the list and print the updated list, followed by a blank line:
Enter the index of a book to take: 1 You took "The Hobbit". ['Project Hail Mary', 'Sherlock Holmes', 'The Hobbit', 'Dune']
Hint: How do you remove something from the list at an index, not by specifying the value? Check the methods in the Helpful Resources section above. Does anything pop out to you?
- Finally, ask the user for the name of a book they would like to leave, and put it at the end of the list. Print out the full list of books again to show what the list looks like now.
What book would you like to leave? The Martian
['Project Hail Mary', 'Sherlock Holmes', 'The Hobbit', 'Dune', 'The Martian']
Example Output (inputs are in italics):
= Little Free Library =
Count: 5 book(s)
['Project Hail Mary', 'The Hobbit', 'Sherlock Holmes', 'The Hobbit', 'Dune']
Enter the title of a book to find: The Hobbit
We have 2 copies of "The Hobbit"
Enter the index of a book to take: 1
You took "The Hobbit"
['Project Hail Mary', 'Sherlock Holmes', 'The Hobbit', 'Dune']
What book would you like to leave? The Martian
['Project Hail Mary', 'Sherlock Holmes', 'The Hobbit', 'Dune', 'The Martian'
Submission
Submit your code to Gradescope. Remember the output must match EXACTLY (including whitespace, blank lines, spelling, and capitalization).