#!/usr/bin/env
# coding:utf-8
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': 0},
{'bookname': '西游记', 'author': '二楼请', 'price': 100, 'number': 5},
{'bookname': '天珠变', 'author': '唐三少', 'price': 100, 'number': 40},
{'bookname': '金瓶梅', 'author': '丁惟宁', 'price': 100, 'number': 40},
{'bookname': '坏蛋是怎样练成的', 'author': '六道', 'price': 100, 'number': 40}
]
print('---------欢迎进入图书管理系统---------------')
book_name_list = [] # 创建列表存储所有书名
for book in library:
book_name_list.append(book['bookname'])
print(book_name_list)
Flag = True
while Flag:
oper = input('\n1. 借书 \n2. 还书 \n3. 查询 \n4. 查询所有 \n5. 退出 \n请选择您需要的服务:')
if oper == '1':
book_list = [] # 创建列表存储所有想借的书
print('1.借书模块')
# 输入书名 ---》
book_name = input('请输入您要借的书:')
for book in library: # 遍历所有书,如果书名和要借的书相同,并书的数量不能为零。
if book_name == book['bookname'] and book['number'] != 0:
book_list.append(book) # 将满足条件的所有书都加入到列表中
# print(book_list)
elif book_name not in book_name_list:
print('查无此书,请重新选择')
break
elif book_name == book['bookname'] and book['number'] == 0:
print('抱歉,这本书已经借出去完了!')
continue
# if not book_list:
# print('')
# else:
# print(book_list)
if len(book_list) != 0:
print(book_list)
if len(book_list) > 1: # 如果列表长度>1,说明有相同名字不同作者的书
author_answer = input('请输入您要哪个作者写的:')
for book in library:
if author_answer == book['author'] and book_name == book['bookname']:
book['number'] -= 1
print('您已经成功借了' + str(author_answer) + '的这本书')
break
else:
print('抱歉,没有此作者写的这本书。')
elif len(book_list) == 1:
for book in library:
if book_name == book['bookname']:
book['number'] -= 1
print('您已经成功借了' + str(book_name) + '这本书')
else: # 说明只有一个作者写的这本书
for book in library:
if book_name == book['bookname']:
book['number'] -= 1
break
elif oper == '2':
Flag1 = True
print('2. 还书模块')
while Flag1:
name = input('请输入您要还的书名:')
author = input('请输入您要还书的作者是:')
for book in library:
if name == book['bookname'] and author == book['author']:
book['number'] += 1
print(str(name) + '归还成功')
Flag1 = False
break
else:
print('书名和作者输入有错误,请从新输入')
elif oper == '3':
print('3.查询模块(书名/作者)')
book_name = input('请输入您要查询的书名:')
for book in library:
if book_name == book['bookname']:
print(book)
elif oper == '4':
print('查询所有模块')
for book in library:
print(f"图书名:{book['bookname']},作者:{book['author']},价格:{book['price']},库存剩余{book['number']}")
elif oper == '5':
print('5.退出')
print('系统退出中....')
time.sleep(1)
print('系统正常退出')
break
else:
print('请重新选择')
下面这个功能其实想加上的,奈何还没怎么看的懂。
输入等待,超时退出
from threading import Timer
import os
input_msg = "啥也没输入"
def work(msg=input_msg):
print("\n你输入信息为:", msg)
os._exit(0) # 执行完成,退出程序
def input_with_timeout(timeout=5):
t = Timer(timeout, work)
t.start()
msg = input("请输入信息:")
input_msg = msg
if len(input_msg) > 0:
t.cancel()
work(msg)
input_with_timeout()