python图书管理系统代码
时间: 2023-09-08 09:15:11 浏览: 69
以下是一个简单的Python图书管理系统代码:
```python
books = []
def add_book():
title = input("Enter book title: ")
author = input("Enter book author: ")
year = input("Enter publication year: ")
book = {"title": title, "author": author, "year": year}
books.append(book)
print("Book added successfully.")
def remove_book():
title = input("Enter book title to remove: ")
for book in books:
if book["title"] == title:
books.remove(book)
print("Book removed successfully.")
return
print("Book not found.")
def view_books():
for book in books:
print(f"{book['title']} by {book['author']} ({book['year']})")
while True:
print("""
1. Add Book
2. Remove Book
3. View Books
4. Exit
""")
choice = input("Enter choice: ")
if choice == "1":
add_book()
elif choice == "2":
remove_book()
elif choice == "3":
view_books()
elif choice == "4":
break
else:
print("Invalid choice. Please try again.")
```
这个程序使用一个包含字典的列表来存储书籍信息。用户可以选择添加、删除或查看书籍。程序使用无限循环和条件语句来实现这些功能。
阅读全文