基于python的图书管理系统源代码
时间: 2023-07-06 07:35:22 浏览: 103
基于Python的图书管理系统
5星 · 资源好评率100%
以下是一个简单的基于 Python 的图书管理系统的源代码。注意,这只是一个示例,实际使用时需要根据需求进行修改和完善。
```python
class Book:
def __init__(self, title, author, isbn):
self.title = title
self.author = author
self.isbn = isbn
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 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 (1-5): ")
print()
if choice == "1":
title = input("Enter book title: ")
author = input("Enter book author: ")
isbn = input("Enter book ISBN: ")
book = Book(title, author, isbn)
library.add_book(book)
print("Book added successfully.")
elif choice == "2":
title = input("Enter book title: ")
book = library.search_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":
library.display_books()
elif choice == "5":
print("Goodbye!")
break
else:
print("Invalid choice. Please try again.")
print()
```
上面的代码定义了两个类:`Book` 和 `Library`。`Book` 类表示一本书,包含标题、作者和 ISBN 号等信息。`Library` 类表示一个图书馆,包含一个书籍列表,以及添加、删除、搜索和显示书籍等方法。在主循环中,用户可以选择进行这些操作。
阅读全文