Repository navigation
Expand file tree
/
Copy pathPyTest4.py
More file actions
55 lines (54 loc) · 1.73 KB
/
Copy pathPyTest4.py
File metadata and controls
55 lines (54 loc) · 1.73 KB
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
53
54
55
#main class
class Book:
def __init__(self, title, author):
self.title = title
self.author = author
self.is_borrowed = False
def borrow(self):
if not self.is_borrowed:
self.is_borrowed = True
print(f"You borrowed '{self.title}' by {self.author}.")
else:
print(f"Sorry, '{self.title}' is already borrowed.")
def return_book(self):
if self.is_borrowed:
self.is_borrowed = False
print(f"You returned '{self.title}' by {self.author}.")
else:
print(f"'{self.title}' was not borrowed.")
#books
books = [
Book("Life of Fred", "Polka dot publising"),
Book("To Kill a Mockingbird", "Harper Lee"),
Book("The Great Gatsby", "F. Scott Fitzgerald")
]
'''
# borrow system old
book1.borrow()
book1.return_book()
book2.borrow()
book2.borrow() # shows already borrowed
book2.return_book()
book3.borrow()
book3.return_book()'''
#borrow system new
while True:
print("\nLibrary Menu")
for i,book in enumerate(books, start=1):
status = "Available" if not book.is_borrowed else "Borrowed"
print(f"{i}. {book.title} by {book.author} ({status})")
choice = input("\nChoose a book number or q to quit: ")
if choice.lower() == 'q':
print("Thank's for using codingal library")
break
if choice.isdigit() and 1 <= int(choice) <= len(books):
book = books[int(choice) - 1]
action = input("Type b to borrow or r to return: ").lower()
if action == 'b':
book.borrow()
elif action == 'r':
book.return_book()
else:
print("Invalid action, please try again.")
else:
print("Invalid action, please try again.")