-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbook_github.py
More file actions
59 lines (45 loc) · 2 KB
/
book_github.py
File metadata and controls
59 lines (45 loc) · 2 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
56
57
58
59
from datetime import datetime, timedelta
def borrow_book(book_title, borrow_days):
"""
Simulates borrowing a book from a library.
Calculates the return date based on the borrowing period.
"""
try:
# Ensure the borrow_days is a positive integer
if borrow_days <= 0:
raise ValueError("The borrowing period must be at least 1 day.")
# Calculate the return date
borrow_date = datetime.now()
## Adds the specified number of days (borrow_days) to the current date to calculate the return date
return_date = borrow_date + timedelta(days=borrow_days)
# Print borrowing details
print(f"\nYou borrowed: '{book_title}'")
print(f"Borrow Date: {borrow_date.strftime('%d-%m-%Y %H:%M:%S')}")
print(f"Return Date: {return_date.strftime('%d-%m-%Y %H:%M:%S')}\n")
except ValueError as ve:
print(f"Error: {ve}")
except Exception as e:
print(f"Unexpected Error: {e}")
def main():
#books we have
books = ["1984", "The Great Gatsby", "To Kill a Mockingbird", "Moby Dick", "The Lord of the Rings"]
print("Welcome to the Library!")
print("Available books:")
for index, book in enumerate(books, start=1):
print(f"{index}. {book}")
try:
# User selects a book
book_choice = int(input("\nEnter the number of the book you want to borrow: "))
if book_choice < 1 or book_choice > len(books):
raise IndexError("Invalid book choice. Please select a valid number.")
# User enters borrowing period
borrow_days = int(input("How many days do you want to borrow the book for? "))
borrow_book(books[book_choice - 1], borrow_days)
except ValueError:
print("Please enter a valid number.")
except IndexError as ie:
print(f"Error: {ie}")
except Exception as e:
print(f"Unexpected Error: {e}")
if __name__ == "__main__":
main()