-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlibrary_management_sys.py
More file actions
77 lines (65 loc) · 2.15 KB
/
library_management_sys.py
File metadata and controls
77 lines (65 loc) · 2.15 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Library Management System
# Variables to hold data
books = [] # List of books (tuples)
book_details = {} # Dictionary to store book details with ISBN as key
# Function to display the menu
def display_menu():
print("\nLibrary Management System")
print("-" * 30)
print("1. Add a Book")
print("2. View All Books")
print("3. Search Book by ISBN")
print("4. Exit")
# Add a book function
def add_book():
isbn = input("Enter ISBN: ")
if isbn in book_details:
print("❗ This book already exists in the library.")
return
title = input("Enter Book Title: ")
author = input("Enter Author Name: ")
year = input("Enter Publication Year: ")
# Store book as a tuple and add it to the list
book = (isbn, title, author, year)
books.append(book)
# Add details to the dictionary
book_details[isbn] = {"Title": title, "Author": author, "Year": year}
print("✅ Book added successfully!")
# View all books
def view_books():
if not books:
print("❗ No books in the library.")
return
print("\nList of Books:")
print("-" * 30)
for book in books:
print(f"ISBN: {book[0]}, Title: {book[1]}, Author: {book[2]}, Year: {book[3]}")
# Search a book by ISBN
def search_book():
isbn = input("Enter ISBN to search: ")
if isbn in book_details:
print("\nBook Details:")
for key, value in book_details[isbn].items():
print(f"{key}: {value}")
else:
print("❌ Book not found.")
# Main function
def main():
while True:
display_menu()
choice = input("Enter your choice: ")
# Switch statement alternative (Python 3.10+ supports match-case)
if choice == "1":
add_book()
elif choice == "2":
view_books()
elif choice == "3":
search_book()
elif choice == "4":
print("📚 Exiting the Library Management System. Goodbye!")
break
else:
print("❌ Invalid choice. Please try again.")
# Entry point
if __name__ == "__main__":
main()