Python Forum
Library Management System - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Library Management System (/thread-44469.html)



Library Management System - annajoy - May-13-2025

Hi everyone,
I'm working on a Python homework assignment to create a simple library management system, and I'm running into some issues. The program needs to:
  • Store books (title, author, ISBN) in a list of dictionaries.
  • Provide a menu to add/remove books, search by title/author, and display all books.
  • Include error handling (e.g., duplicate ISBNs, book not found).
library = []

def add_book(title, author, isbn):
    for book in library:
        if book['isbn'] == isbn:
            print("Error: ISBN already exists!")
            return
    book = {'title': title, 'author': author, 'isbn': isbn}
    library.append(book)
    print("Book added successfully!")

def display_books():
    if not library:
        print("No books in the library.")
    else:
        print("Library Books:")
        for i, book in enumerate(library, 1):
            print(f"{i}. {book['title']} by {book['author']} (ISBN: {book['isbn']})")

# Incomplete menu
while True:
    print("\nLibrary Management System")
    print("1. Add a book")
    print("2. Remove a book")
    print("3. Search for a book")
    print("4. Display all books")
    print("5. Exit")
    choice = input("Enter your choice (1-5): ")
    if choice == '1':
        title = input("Enter book title: ")
        author = input("Enter author: ")
        isbn = input("Enter ISBN: ")
        add_book(title, author, isbn)
    elif choice == '4':
        display_books()
    elif choice == '5':
        break
    else:
        print("Invalid choice!")
I’m struggling with the remove_book function. How do I remove a book by ISBN and handle cases where the ISBN doesn’t exist? For the search_book function, I want to search by title or author (partial matches), but I’m not sure how to implement this efficiently. Is my error handling sufficient, or should I add more checks (e.g., for invalid ISBN formats)? Any tips on making the menu more user-friendly?
Thanks!


RE: Library Management System - deanhystad - May-13-2025

Dictionaries are efficient for searching. Would be a good choice for your menu too