Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

Can you write me a code for a library management

  1. User

    Can you write me a code for a library management

  2. Assistant

    Certainly! Here's a basic implementation of a library management system in Python:

    class Library:
        def __init__(self, name):
            self.name = name
            self.books = []
    
        def add_book(self, book):
            self.books.append(book)
            print(f"Book '{book}' has been added to the library.")
    
        def remove_book(self, book):
            if book in self.books:
                self.books.remove(book)
                print(f"Book '{book}' has been removed from the library.")
            else:
                print(f"Book '{book}' is not currently available in the library.")
    
        def display_books(self):
            print(f"Books available in '{self.name}' library:")
            if not self.books:
                print("No books available.")
            else:
                for book in self.books:
                    print(book)
    
    
    library = Library("My Library")
    
    while True:
        print("\n====== Library Management System ======")
        print("1. Add Book")
        print("2. Remove Book")
        print("3. Display Books")
        print("4. Exit")
    
        choice = input("Enter your choice (1-4): ")
    
        if choice == "1":
            book_name = input("Enter the name of the book: ")
            library.add_book(book_name)
        elif choice == "2":
            book_name = input("Enter the name of the book: ")
            library.remove_book(book_name)
        elif choice == "3":
            library.display_books()
        elif choice == "4":
            print("Thank you for using the Library Management System!")
            break
        else:
            print("Invalid choice. Please try again.")
    

    This code defines a Library class with methods for adding, removing, and displaying books. It then provides a simple command-line interface for users to interact with the library system.