import cards_tool# 主业务逻辑cards_tool.read_card() # 1.显示用户主界面while True: cards_tool.show_menu() menu_str = input("请选择执行的操作:") print("您选择的功能:%s" % menu_str) if menu_str == "1": cards_tool.create_card() elif menu_str == "2": cards_tool.show_card_all() elif menu_str == "3": cards_tool.search_card() elif menu_str == "0": cards_tool.write_card() break else: print("输入有误,请重新输入")
import oscard_info_all = [] # 所有名片信息# 名片管理 具体功能def write_card(): """退出系统时,将名片信息保存到文件夹中""" f = open("E://workspace_python//FirstDemo//名片管理系统.txt", "w") f.write(str(card_info_all)) f.close() def read_card(): """进入系统时,将名片信息加载到内存中""" is_exist = os.path.exists("E://workspace_python//FirstDemo//名片管理系统.txt") if is_exist: # 文件存在将信息加载到内存中 f = open("E://workspace_python//FirstDemo//名片管理系统.txt","r") global card_info_all card_info_all = eval(f.read()) # print(card_info_all) f.close() else: # 没有文件创建空文件 f = open("E://workspace_python//FirstDemo//名片管理系统.txt", "w") f.close() print(is_exist) def show_menu(): """主菜单页面展示""" print("*"*30) print("欢迎使用[命名管理系统] v1.0") print() print("1.新建名片") print("2.显示全部") print("3.查询名片") print() print("0.退出系统") print("*"*30) def create_card(): """新建名片""" print("功能:新建名片") name = input("请输入姓名:") phone = input("请输入电话:") qq = input("请输入qq号:") email = input("请输入邮箱:") card_info = {"name":name,"phone":phone, "qq":qq, "email":email} card_info_all.append(card_info) print("添加%s的名片成功" % name) def show_card_all(): """显示所有名片""" print("功能:显示全部") if not len(card_info_all): print("没有任何名片") return print("name".ljust(14),"phone".ljust(14),"qq".ljust(14),"email".ljust(14),sep="") print("-"*56) for card in card_info_all: print(card["name"].ljust(14),card["phone"].ljust(14),card["qq"].ljust(14),card["email"].ljust(14),sep="") print("-"*56) def search_card(): """查询名片""" print("功能:查询名片") name = input("请输入查询的姓名:") for card in card_info_all: if name == card["name"]: print("name".ljust(14), "phone".ljust(14), "qq".ljust(14), "email".ljust(14), sep="") print("-" * 56) print(card["name"].ljust(14), card["phone"].ljust(14), card["qq"].ljust(14), card["email"].ljust(14),sep="") # 名片高级处理 set_card(card) break; else: print("没有找到%s" % name) def set_card(card): """名片的高级设置""" while True: menu = input("请输入对名片的操作:1.修改/ 2.删除/ 0.返回上一级") if menu == "1": name = input("请输入姓名:") phone = input("请输入电话:") qq = input("请输入qq号:") email = input("请输入邮箱:") card["name"] = name card["phone"] = phone card["qq"] = qq card["email"] = email print("%s 的名片修改成功" % name) return elif menu == "2": card_info_all.remove(card) print("删除名片成功") return elif menu == "0": return else: print("输入有误,请重新输入")