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 17:27:20 浏览: 135
Book.class
这段代码是一个图书管理系统的实现,其中定义了两个类 Book 和 Library。 Book 表示一本书,具有三个属性 title、author 和 isbn;Library 表示一个图书馆,具有一个 books 列表属性,分别实现了添加、删除、搜索和展示书籍的功能。在主程序中,使用了一个 while 循环来不断接收用户的输入,根据用户的输入来调用不同的方法实现对书籍的管理。
阅读全文