编写一个简单的图书馆里系统,能够实现图书的借阅,还书,查找,退出系统这几项功能。
'''
图书馆里系统
至少5本书
library = [{'bookname':'XXX','author':'XXX','price':1000,number:4},{},{},{}]
1.借书
2.还书
3.查询
4.查看所有
5.退出
'''
import time
library = [{'bookname': '红楼梦', 'author': '曹雪芹', 'price': 100, 'number': 40},
{'bookname': '水浒传', 'author': '施耐庵', 'price': 100, 'number': 40},
{'bookname': '西游记', 'author': '吴承恩', 'price': 100, 'number': 40},
{'bookname': '三国演义', 'author': '罗贯中', 'price': 100, 'number': 40},
{'bookname': '红楼梦', 'author': '李三', 'price': 100, 'number': 40}]
while True:
choice = input('\n1.借书\n2.还书\n3.查询\n4.查看所有\n5.退出\n请选择你需要的服务:')
if choice == '1': # 借书
print('借书')
lookname = input('输入书名:')
num = 0
for book in library:
if lookname == book.get('bookname'):
book_borrow.append('book')
look_author = book.get('author')
num += 1 # 得到重书名个数
if num == 0:
print('不存在此书。')
elif num == 1:
print('存在此书')
else:
look_author = input('根据作者选择要借的书:')
for book in library:
if book['number'] > 1:
if lookname == book['bookname'] and look_author == book['author']: # 书名和作者都对
book['number'] -= 1
book_borrow.append('book')
print('借阅成功。')
break
else:
print('此书已经被借完。')
elif choice == '2': # 还书
print('还书')
back_name = input('输入要还的书名')
back_author = input('书的作者')
for book in library:
if back_name == book.get('bookname') and back_author == book.get('author'):
if book.get('number') < 40:
book['number'] += 1
else:
print('藏书已满,不需要还书。')
elif choice == '3': # 查询
choice_lookfor = input('查询书名(1) or 作者(2):')
if choice_lookfor == '1':
namelook = input('你要查询的书名:')
for book in library:
if choice_lookfor in book:
# if book['bookname'] == namelook:
print('该书存在:' + f"书名:{book.get('bookname')}\t,作者:{book.get('author')}\t,"
f"数量:{book.get('number')}")
elif choice_lookfor == '2':
authorlook = input('你要查询的作者名:')
for book in library:
if book.get('author') == authorlook:
print('该书存在:' + f"书名:{book.get('bookname')}\t,作者:{book.get('author')}\t,"
f"数量:{book.get('number')}")
else:
print('你打错了。')
elif choice == '4':
print('查看所有信息。\n' + '*' * 50)
print('图书名\t 作者\t 数量')
for book in library:
print(book.get('bookname') + '\t', book.get('author') + '\t', book.get('number'))
# print(f"图书名:{book.get('bookname')},作者:{book.get('author')},"
# f"价格:{book.get('price')},库存剩余:{book.get('number')}")
elif choice == '5':
print('退出系统')
time.sleep(2)
break
else:
print('输入错误,请重新输入。')