-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
52 lines (37 loc) · 1.51 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Write a Library class with no_of_books and books as two instance variables.
# Write a program to create a library from this Library class and show how you can print all books, add a book and get the number of books using different methods.
# Show that your program doesn't persist the books after the program is stopped!
class Library:
def __init__(self):
self.books: list[str] = []
self.no_of_books: int = 0
def print_books(self) -> None:
for book in self.books:
print(f'- {book}')
def add_book(self, new_book: str) -> None:
self.books.append(new_book)
self.no_of_books += 1
def get_no_of_books(self) -> int:
return self.no_of_books
def main() -> None:
library: Library = Library() # Creates an instance of Library.
# Prints initial library details.
print(f'Library has {library.get_no_of_books()} books, which are: ')
library.print_books()
print()
while True:
# Inputs a book to add to library.
book: str = input('Enter a book to add to library or enter q to quit: \n')
if book.lower() == 'q':
break
if not book:
print('Enter a book to add to Library!\n')
continue
library.add_book(book)
print(f"'{book}' book has been added to the library.\n")
# Prints final details if user quits the program.
print()
print(f'Library has {library.get_no_of_books()} books, which are: ')
library.print_books()
if __name__ == '__main__':
main()