This program will simulate a small "database" of information about books at a university. The information for each book will be stored as an object of the Book class, and all the Book objects will be stored in a list. With this program, you will be able to:
- See a list of books
- Add a new book to the database
- Fetch more detailed information about a book
To do this, write a code file called book.py which will define the Book type so that it can be used in the program below and produce output as detailed in the output section.
A few points to remember while working:
- Before starting to write your code in book.py, read the code provided below, so that you can see what the Book class will need, in order to be compatible with the code below.
- You may also wish to save the code below in a separate file (in the same folder as book.py), and you can run it for testing purposes.
- Because you are using objects with instance data, you will not need dictionaries in your Book class code.
- To ensure that your code is working correctly, look at the sample output below and see if your own output is the same or similar.
- For this homework assignment, we will not be deleting books, since their id numbers will be tied directly to their positions in the list.
+ Expand Code
NOTE: The code below would go in a file separate from book.py! It is merely for testing the Book class that you will write.
# Execute file containing class code
exec(open("book.py").read())
# An empty list, when the program first
# starts. As the user starts to add
# books, it will be filled with items --
# those items being objects whose instance
# data is book information.
books = []
choice = None
while choice != "0":
print(
"""
Create Books
...
- Collapse Code
NOTE: The code below would go in a file separate from book.py! It is merely for testing the Book class that you will write.
# Execute file containing class code
exec(open("book.py").read())
# An empty list, when the program first
# starts. As the user starts to add
# books, it will be filled with items --
# those items being objects whose instance
# data is book information.
books = []
choice = None
while choice != "0":
print(
"""
Create Books
0 - Quit
1 - List All Books
2 - Add a Book
3 - Display Book Information
"""
)
choice = input("Choice: ")
print()
# exit
if choice == "0":
print("Good-bye.")
# display books
elif choice == "1":
print("Books:\n")
for book in books:
print (book.basic_info())
# add a book
elif choice == "2":
title = input ("What is the title?: ")
author = input ("Who is the author?: ")
pub_year = int (input ("Year of publication?: "))
pages = int (input ("How many pages?: "))
new_book = Book(len(books), title, author, pub_year, pages)
books.append(new_book)
# print info about a book
elif choice == "3":
print("Books:\n")
for book in books:
print (book.basic_info())
print()
id = int (input ("Which number book do you want?: "))
print()
print (books[id])
# some unknown choice
else:
print("Sorry, but", choice, "isn't a valid choice.")
print()
input("\n\nPress the enter key to exit.")
- Collapse Code
Note that because of user input and random values, the output will look different each time you run the program.
Program output example:
+ Expand Output
Create Books
0 - Quit
1 - List All Books
2 - Add a Book
3 - Display Book Information
Choice: 1
Books:
Create Books
0 - Quit
...
- Collapse Output
Create Books
0 - Quit
1 - List All Books
2 - Add a Book
3 - Display Book Information
Choice: 1
Books:
Create Books
0 - Quit
1 - List All Books
2 - Add a Book
3 - Display Book Information
Choice: 2
What is the title?: Wuthering Heights
Who is the author?: Emily Bronte
Year of publication?: 1847
How many pages?: 256
Added Wuthering Heights
Create Books
0 - Quit
1 - List All Books
2 - Add a Book
3 - Display Book Information
Choice: 2
What is the title?: Jane Eyre
Who is the author?: Charlotte Bronte
Year of publication?: 1847
How many pages?: 624
Added Jane Eyre
Create Books
0 - Quit
1 - List All Books
2 - Add a Book
3 - Display Book Information
Choice: 2
What is the title?: Pride and Prejudice
Who is the author?: Jane Austen
Year of publication?: 1813
How many pages?: 272
Added Pride and Prejudice
Create Books
0 - Quit
1 - List All Books
2 - Add a Book
3 - Display Book Information
Choice: 1
Books:
0 : Wuthering Heights
1 : Jane Eyre
2 : Pride and Prejudice
Create Books
0 - Quit
1 - List All Books
2 - Add a Book
3 - Display Book Information
Choice: 3
Books:
0 : Wuthering Heights
1 : Jane Eyre
2 : Pride and Prejudice
Which number book do you want?: 2
title : Pride and Prejudice
pages : 272
year : 1813
author : Jane Austen
Create Books
0 - Quit
1 - List All Books
2 - Add a Book
3 - Display Book Information
Choice: 2
What is the title?: Frankenstein
Who is the author?: Mary Shelley
Year of publication?: 1818
How many pages?: 166
Added Frankenstein
Create Books
0 - Quit
1 - List All Books
2 - Add a Book
3 - Display Book Information
Choice: 3
Books:
0 : Wuthering Heights
1 : Jane Eyre
2 : Pride and Prejudice
3 : Frankenstein
Which number book do you want?: 0
title : Wuthering Heights
pages : 256
year : 1847
author : Emily Bronte
Create Books
0 - Quit
1 - List All Books
2 - Add a Book
3 - Display Book Information
Choice: 3
Books:
0 : Wuthering Heights
1 : Jane Eyre
2 : Pride and Prejudice
3 : Frankenstein
Which number book do you want?: 1
title : Jane Eyre
pages : 624
year : 1847
author : Charlotte Bronte
Create Books
0 - Quit
1 - List All Books
2 - Add a Book
3 - Display Book Information
Choice: 0
Good-bye.
Press the enter key to exit.
- Collapse Output
Questions: - How did the process of creating these programs go for you?
- What were your main challenges and how did you overcome them?
- What did you learn that may be of use as you move along in this class?