1 # 用來存儲名片的列表 2 card_infors = [] 3 4 5 def print_menu(): 6 # 1. 打印功能提示 7 print("=" * 50) 8 print(" 名片管理系統") 9 print("1. 增長一個新名片") 10 print("2. 刪除一個名稱") 11 print("3. 修改一個名片") 12 print("4. 查詢一個名片") 13 print("5. 顯示全部名片") 14 print("6. 保存信息") 15 print("7. 退出系統") 16 print("=" * 50) 17 18 19 def add_new_card_infor(): 20 """完成文檔輸入:""" 21 new_name = input("請輸入新的名字:") 22 new_qq = input("請輸入新的QQ:") 23 new_weixin = input("請輸入新的微信:") 24 new_addr = input("請輸入地址:") 25 26 # 定義一個新的字典,用來存儲新的名片 27 new_infor = {} 28 new_infor['name'] = new_name 29 new_infor['qq'] = new_qq 30 new_infor["weixin"] = new_weixin 31 new_infor['addr'] = new_addr 32 global card_infors 33 card_infors.append(new_infor) 34 35 36 def find_card_infor(): 37 find_name = input("請輸入要查找的姓名:") 38 39 find_flag = 0 # 默認沒有找到 40 41 global card_infors 42 for temp in card_infors: 43 if find_name == temp["name"]: 44 print("%s\t%s\t%s\t%s" % (temp['name'], temp['qq'], temp['weixin'], temp['addr'])) 45 find_flag = 1 46 break 47 if find_flag == 0: 48 print("查無此人") 49 50 51 def show_all_infor(): 52 """顯示全部信息""" 53 print("姓名\tQQ\t微信\t住址") 54 global card_infors 55 for temp in card_infors: 56 # print(temp) 57 print("%s\t%s\t%s\t%s" % (temp['name'], temp['qq'], temp['weixin'], temp['addr'])) 58 59 60 def del_card_infor(): 61 del_name = input("請輸入要查找的姓名:") 62 find_flag = 0 # 默認沒找到 63 64 global card_infors 65 for temp in card_infors: 66 if del_name == temp["name"]: 67 card_infors.remove(temp) 68 find_flag = 1 69 show_all_infor() 70 break 71 if find_flag == 0: 72 print("查無此人") 73 74 75 def del_modify_infor(): 76 modify_name = input("請輸入要修改信息的名稱:") 77 find_flag = 0 78 79 global card_infors 80 for temp in card_infors: 81 if modify_name == temp["name"]: 82 modify_content = input("請輸入要修改的信息,如name,qq,weixin,addr等:") 83 for name in temp.keys(): 84 print("+" * 50) 85 print(temp.keys()) 86 print("+" * 50) 87 print(name) 88 if modify_content == name: 89 modify_value = input("請輸入要修改的值:") 90 temp[name] = modify_value 91 show_all_infor() 92 find_flag = 1 93 break 94 95 if find_flag == 0: 96 print("查無此人") 97 98 def save_2_file(): 99 """把已經添加的信息保存到指定文件中""" 100 f = open("backup.data","w") #data是本身定義的格式 101 f.write(str(card_infors)) 102 f.close() 103 104 # 加載以前數據 105 def load_infor(): 106 f = open("backup.data") 107 108 global card_infors # 在局部變量中修改全局變量時使用:global 109 card_infors = eval(f.read()) #eval恢復數據的原始模式,即list模式 110 111 f.close() 112 113 114 def main(): 115 """完成對整個程序得控制""" 116 117 #恢復(加載)以前的數據到程序中 118 load_infor() 119 120 print_menu() 121 while True: 122 # 2. 獲取用戶的輸入 123 num = int(input("請輸入操做序號:")) 124 125 # 3. 根據用戶的數據執行相應的功能: 126 if num == 1: 127 add_new_card_infor() 128 elif num == 2: 129 del_card_infor() 130 elif num == 3: 131 del_modify_infor() 132 elif num == 4: 133 find_card_infor() 134 elif num == 5: 135 show_all_infor() 136 elif num == 6: 137 save_2_file() 138 elif num == 7: 139 break 140 else: 141 print("輸入有誤,請從新輸入:") 142 143 144 if __name__ == "__main__": 145 main() 146 147 # 調用主函數