class Book:
def __init__(self, title, author, genre):
self.title = title
self.author = author
self.genre = genre
self.is_available = True
class Library:
def __init__(self):
self.books = []
def add_book(self, book):
self.books.append(book)
def remove_book(self, book):
self.books.remove(book)
def display_books(self):
if self.books:
for book in self.books:
print(f"Title: {book.title}, Author: {book.author}, Genre: {book.genre}, Available: {book.is_available}")
else:
print("No books in the library!")
def find_book(self, title):
for book in self.books:
if book.title.lower() == title.lower():
return book
return None
def check_out_book(self, title):
book = self.find_book(title)
if book:
if book.is_available:
book.is_available = False
print(f"Successfully checked out '{book.title}'.")
else:
print(f"'{book.title}' is already checked out.")
else:
print(f"'{title}' not found in the library.")
def return_book(self, title):
book = self.find_book(title)
if book:
if not book.is_available:
book.is_available = True
print(f"Successfully returned '{book.title}'.")
else:
print(f"'{book.title}' is already available.")
else:
print(f"'{title}' not found in the library.")
# Example usage
library = Library()
book1 = Book("Python Crash Course", "Eric Matthes", "Programming")
book2 = Book("The Great Gatsby", "F. Scott Fitzgerald", "Fiction")
book3 = Book("To Kill a Mockingbird", "Harper Lee", "Fiction")
library.add_book(book1)
library.add_book(book2)
library.add_book(book3)
library.display_books()
library.check_out_book("The Great Gatsby")
library.check_out_book("The Catcher in the Rye")
library.display_books()
library.return_book("The Great Gatsby")
library.return_book("The Catcher in the Rye")
library.display_books()
Book
and Library
. The Book
class represents a single book with its title, author, genre, and availability status. The Library
class manages a collection of books and provides methods to add, remove, display, check out, and return books.Lods salamat try koSure! I can help you with the code for a library management system in Python. Here's a simple implementation to get you started:
Python:class Book: def __init__(self, title, author, genre): self.title = title self.author = author self.genre = genre self.is_available = True class Library: def __init__(self): self.books = [] def add_book(self, book): self.books.append(book) def remove_book(self, book): self.books.remove(book) def display_books(self): if self.books: for book in self.books: print(f"Title: {book.title}, Author: {book.author}, Genre: {book.genre}, Available: {book.is_available}") else: print("No books in the library!") def find_book(self, title): for book in self.books: if book.title.lower() == title.lower(): return book return None def check_out_book(self, title): book = self.find_book(title) if book: if book.is_available: book.is_available = False print(f"Successfully checked out '{book.title}'.") else: print(f"'{book.title}' is already checked out.") else: print(f"'{title}' not found in the library.") def return_book(self, title): book = self.find_book(title) if book: if not book.is_available: book.is_available = True print(f"Successfully returned '{book.title}'.") else: print(f"'{book.title}' is already available.") else: print(f"'{title}' not found in the library.") # Example usage library = Library() book1 = Book("Python Crash Course", "Eric Matthes", "Programming") book2 = Book("The Great Gatsby", "F. Scott Fitzgerald", "Fiction") book3 = Book("To Kill a Mockingbird", "Harper Lee", "Fiction") library.add_book(book1) library.add_book(book2) library.add_book(book3) library.display_books() library.check_out_book("The Great Gatsby") library.check_out_book("The Catcher in the Rye") library.display_books() library.return_book("The Great Gatsby") library.return_book("The Catcher in the Rye") library.display_books()
This code defines two classes:Book
andLibrary
. TheBook
class represents a single book with its title, author, genre, and availability status. TheLibrary
class manages a collection of books and provides methods to add, remove, display, check out, and return books.
In the example usage at the end, it demonstrates how to create a library, add books to it, display the books, check out a book, return a book, and display the updated list of books.
Feel free to modify the code according to your specific requirements and add more functionality as needed.
You're welcome! I'm glad to hear that you'll be trying the code. If you have any questions or need further assistance, feel free to ask. Good luck with your library management project in Python!Lods salamat try ko