class Book: def__init__(self,title, author, isbn): selftitle=title selfauthor=author self.isbn=isbn class Library: def__init__(self): self.books=[] defadd_book(self,book): self.books.append(book) defremove_book(self,book): self.books.remove(book) def search_book(self,title): for book in self.books: if book.title==title: return book return None def display_books(self): for book in self.books: print(f"{book.title} by {book.author} (ISBN: {book.isbn})") library=Library() while True: print("1. Add book") print("2. Remove book") print("3. Search book") print("4. Display all books") print("5. Exit") choice=input("Enter your choice:") if choice== "1": title=input("Enter book title:") author=input("Enter book author") isbn=input("Enter bookISBN:") book=Book(title,author,isbn) library.add_book(book) print("Book added successfully!") elif choice=="2": title=input("Enter book title:") book=librarysearch_book(title) if book: library.remove_book(book) print("Book removed successfully!") else: print("Book not found!") elif choice == "3": title=input("Enter book title:") book=library.search_book(title) if book: print(f"{book.title} by {book.author} (ISBN: {book.isbn})") else: print("Book not found!") elif choice == "4": librarydisplay_books() elif choice=="5": break else: print("Invalid choice!")
时间: 2024-04-20 20:27:20 浏览: 146
这段代码是一个图书管理系统的实现,其中定义了两个类 Book 和 Library。 Book 表示一本书,具有三个属性 title、author 和 isbn;Library 表示一个图书馆,具有一个 books 列表属性,分别实现了添加、删除、搜索和展示书籍的功能。在主程序中,使用了一个 while 循环来不断接收用户的输入,根据用户的输入来调用不同的方法实现对书籍的管理。
相关问题
class Book: def __init__(self, title, author, isbn, available=True): self.title = title self.author = author self.isbn = isbn self.available = available class Library: def __init__(self, books=None): if books is None: books = [] self.books = books def add_book(self, book): self.books.append(book) def remove_book(self, book): self.books.remove(book) def search_book_by_title(self, title): return [book for book in self.books if book.title == title] def search_book_by_author(self, author): return [book for book in self.books if book.author == author] def search_book_by_isbn(self, isbn): return [book for book in self.books if book.isbn == isbn] def checkout_book(self, book): if book.available: book.available = False return True else: return False def return_book(self, book): book.available = True if __name__ == '__main__': library = Library() book1 = Book('The Great Gatsby', 'F. Scott Fitzgerald', '978-0143129453') book2 = Book('To Kill a Mockingbird', 'Harper Lee', '978-0446310789') book3 = Book('1984', 'George Orwell', '978-0451524935') library.add_book(book1) library.add_book(book2) library.add_book(book3) print(library.search_book_by_title('1984'))修正优化
class Book:
def __init__(self, title, author, isbn, available=True):
self.title = title
self.author = author
self.isbn = isbn
self.available = available
class Library:
def __init__(self, books=None):
if books is None:
books = []
self.books = books
def add_book(self, book):
self.books.append(book)
def remove_book(self, book):
self.books.remove(book)
def search_book_by_title(self, title):
return [book for book in self.books if book.title.lower() == title.lower()]
def search_book_by_author(self, author):
return [book for book in self.books if book.author.lower() == author.lower()]
def search_book_by_isbn(self, isbn):
return [book for book in self.books if book.isbn == isbn]
def checkout_book(self, book):
if book.available:
book.available = False
return True
else:
return False
def return_book(self, book):
book.available = True
if __name__ == '__main__':
library = Library()
book1 = Book('The Great Gatsby', 'F. Scott Fitzgerald', '978-0143129453')
book2 = Book('To Kill a Mockingbird', 'Harper Lee', '978-0446310789')
book3 = Book('1984', 'George Orwell', '978-0451524935')
library.add_book(book1)
library.add_book(book2)
library.add_book(book3)
print(library.search_book_by_title('1984'))
# 修正优化:
# 1. 在搜索图书时,将标题和作者名转换为小写字母,以避免大小写不匹配的问题。
# 2. 在搜索图书时,如果没有找到符合条件的图书,则返回一个空列表,而不是 None。
对以下代码的每一行进行解释class Book: def__init__(self,title, author, isbn): selftitle=title selfauthor=author self.isbn=isbn class Library: def__init__(self): self.books=[] defadd_book(self,book): self.books.append(book) defremove_book(self,book): self.books.remove(book) def search_book(self,title): for book in self.books: if book.title==title: return book return None def display_books(self): for book in self.books: print(f"{book.title} by {book.author} (ISBN: {book.isbn})") library=Library() while True: print("1. Add book") print("2. Remove book") print("3. Search book") print("4. Display all books") print("5. Exit") choice=input("Enter your choice:") if choice== "1": title=input("Enter book title:") author=input("Enter book author") isbn=input("Enter bookISBN:") book=Book(title,author,isbn) library.add_book(book) print("Book added successfully!") elif choice=="2": title=input("Enter book title:") book=librarysearch_book(title) if book: library.remove_book(book) print("Book removed successfully!") else: print("Book not found!") elif choice == "3": title=input("Enter book title:") book=library.search_book(title) if book: print(f"{book.title} by {book.author} (ISBN: {book.isbn})") else: print("Book not found!") elif choice == "4": librarydisplay_books() elif choice=="5": break else: print("Invalid choice!")
这段代码定义了两个类:`Book` 和 `Library`。`Book`类有一个构造函数 `__init__`,它有三个参数:`title`、`author` 和 `isbn`。在构造函数中,这些参数被分别赋值给对象的属性 `self.title`、`self.author` 和 `self.isbn`。`Library`类也有一个构造函数 `__init__`,它初始化了一个空列表 `self.books`。此外,`Library`类还有 `add_book`、`remove_book`、`search_book` 和 `display_books` 四个方法。
`add_book` 方法接受一个 `Book` 对象作为参数,将其添加到 `self.books` 列表中。
`remove_book` 方法接受一个 `Book` 对象作为参数,将其从 `self.books` 列表中删除。
`search_book` 方法接受一个书名 `title` 作为参数,遍历 `self.books` 列表寻找匹配的书名。如果找到,返回该书的 `Book` 对象;否则返回 `None`。
`display_books` 方法遍历 `self.books` 列表,打印出每本书的标题、作者和 ISBN 号。
在主程序中,首先创建了一个 `Library` 对象 `library`。然后通过循环打印出菜单,等待用户输入操作。如果用户选择添加书籍,则提示用户输入书籍的标题、作者和 ISBN 号,创建一个 `Book` 对象并将其添加到 `library` 中。如果用户选择删除书籍,则提示用户输入书籍的标题,查找匹配的书籍并将其从 `library` 中删除。如果用户选择搜索书籍,则提示用户输入书籍的标题,查找匹配的书籍并打印其标题、作者和 ISBN 号。如果用户选择显示所有书籍,则调用 `library` 的 `display_books` 方法。如果用户选择退出,则退出程序。如果用户输入了无效的选项,则提示用户重新输入。
阅读全文