编写一个简单的图书馆里系统,能够实现图书的借阅,还书,查找,退出系统几项功能。 假设图书馆有6种书,每种书是一个字典,键有书名、作者、价格、数量。使用Python函数的方式。
时间: 2024-11-25 09:20:39 浏览: 26
好的,我们可以创建一个简单的图书管理系统,主要包含以下几个函数:
```python
class Library:
def __init__(self):
self.books = {
"Book1": {"title": "小说A", "author": "作者A", "price": 50, "quantity": 3},
"Book2": {"title": "小说B", "author": "作者B", "price": 40, "quantity": 2},
# ... 其他4本书类似,省略
}
self.borrowed_books = {}
def display_books(self):
for book_name, details in self.books.items():
print(f"{book_name} - {details['title']} by {details['author']}, Price: {details['price']}, Quantity: {details['quantity']}")
def borrow_book(self, book_name):
if book_name in self.books and self.books[book_name]['quantity'] > 0:
self.borrowed_books[book_name] = self.books.pop(book_name)
print(f"成功借阅 {book_name}")
else:
print("书籍不可用或已全部借出")
def return_book(self, book_name):
if book_name in self.borrowed_books:
self.books[book_name] = self.borrowed_books.pop(book_name)
print(f"成功归还 {book_name}")
else:
print("未找到该借出的书籍")
def search_book(self, title):
matches = [book for book, details in self.books.items() if details['title'] == title]
if matches:
print(f"找到了书名匹配的书籍:{', '.join(matches)}")
else:
print("未找到相符的书籍")
def exit_system(self):
print("感谢使用图书管理系统,再见!")
self.borrowed_books.clear()
return
# 使用示例
library = Library()
library.display_books()
# 借书
library.borrow_book("小说A")
# 查找
library.search_book("作者B")
# 还书
library.return_book("小说B")
# 退出系统
library.exit_system()
```
在这个简单系统中,我们首先定义了一个`Library`类,其中包含了书的信息以及借阅、还书、查找和退出的功能。用户可以调用这些方法来进行操作。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)