import tools
while True:
tools.show_menu()
action = input("请选择操作功能:")
print("您选择的操作是:【%s】" % action)
if action in ["1", "2", "3"]:
if action == "1":
tools.new_book()
if action == "2":
tools.show_book()
if action == "3":
tools.search_book()
elif action == "0":
print("欢迎再次使用!")
break
else:
print("非法!")
工具包模块:
book_list = []
def show_menu():
print("*" * 50)
print("欢迎使用该系统!")
print("*" * 50)
print("【1】新增图书")
print("【2】全部图书")
print("【3】查询图书")
print("【0】退出系统")
def new_book():
print("*" * 50)
print("新增图书")
book_name = input("请输入书名:")
book_writer = input("请输入作者:")
book_price = input("请输入价格:")
book_dict = {"name": book_name,
"writer": book_writer,
"price": book_price}
book_list.append(book_dict)
print(book_list)
print("添加《%s》成功" % book_name)
def show_book():
print("*" * 50)
print("显示图书")
if len(book_list) == 0:
print("当前无!")
return
for name in ["书名", "作者", "价格"]:
print(name, end="\t\t")
print()
print("-" * 50)
for book_dict in book_list:
print("%s\t\t%s\t\t%s" % (book_dict["name"],
book_dict["writer"],
book_dict["price"]))
def search_book():
print("*" * 50)
print("查询图书")
find_book = input("请输入要找的书:")
for book_dict in book_list:
if book_dict["name"] == find_book:
print("书名\t\t作者\t\t价格")
print("%s\t\t%s\t\t%s" % (book_dict["name"],
book_dict["writer"],
book_dict["price"]))
deal_book(book_dict)
break
else:
print("没有你要找的书!")
def deal_book(find_dict):
deal_action = input("请选择操作:"
"【1】修改【2】删除【0】返回 :")
if deal_action == "1":
find_dict["name"] = input_new(find_dict["name"], "书名:")
find_dict["writer"] = input_new(find_dict["writer"], "作者:")
find_dict["price"] = input_new(find_dict["price"], "价格:")
print("修改成功!")
if deal_action == "2":
book_list.remove(find_dict)
print("已删除!")
if deal_action == "0":
return
def input_new(dict_value, tip):
result = input(tip)
if len(result) > 0:
return result
else:
return dict_value