Python系列(1)——函数的应用之名片管理系统
1 card_list = [] 2 3 4 def main_desk(): 5 print('*'*50) 6 print('欢迎使用【名片管理系统】V1.0') 7 print('1.新建名片') 8 print('2.显示全部') 9 print('3.查询名片') 10 print('0.退出系统') 11 print('*'*50) 12 13 14 def new_card(): 15 print('新建名片') 16 print('='*50) 17 name_str = input('姓名:') 18 age_str = input('年龄:') 19 phone_str = input('电话:') 20 card_dict = {'name': name_str, 'age': age_str, 'phone': phone_str} 21 card_list.append(card_dict) 22 print(card_dict) 23 print(f'添加{name_str}名片成功') 24 25 26 def show_all(): 27 print('显示全部名片') 28 print('=' * 50) 29 if len(card_list) == 0: 30 print('名片为空,请先新建名片') 31 return 32 print('姓名\t\t年龄\t\t电话') 33 for card_dict in card_list: 34 print(f'{card_dict["name"]}\t\t{card_dict["age"]}\t\t{card_dict["phone"]}') 35 36 37 def search_card(): 38 print('查询名片') 39 find_name = input('请输入查找的姓名:') 40 for card_dict in card_list: 41 if card_dict['name'] == find_name: 42 print('='*50) 43 print('姓名\t\t年龄\t\t电话') 44 print(f'{card_dict["name"]}\t\t{card_dict["age"]}\t\t{card_dict["phone"]}') 45 deal_card(card_dict) 46 break 47 else: 48 print('名片不存在,请重新输入') 49 50 51 def deal_card(find_dict): 52 action = input('请输入操作:【1】修改【2】删除【0】返回菜单:') 53 if action == '1': 54 find_dict['name'] = work(find_dict['name'], '姓名:') 55 find_dict['age'] = work(find_dict['age'], '年龄:') 56 find_dict['phone'] = work(find_dict['phone'], '电话:') 57 print('修改名片成功') 58 elif action == '2': 59 card_list.remove(find_dict) 60 print('删除成功') 61 62 63 def work(dict_value, message): 64 result_str = input(message) 65 if len(result_str) > 0: 66 return result_str 67 else: 68 return dict_value 69 70 71 while True: 72 main_desk() 73 action_str = input('请输入您的操作:') 74 print(f'您的操作是【{action_str}】') 75 if action_str == '1': 76 new_card() 77 elif action_str == '2': 78 show_all() 79 elif action_str == '3': 80 search_card() 81 pass 82 elif action_str == '0': 83 print('欢迎您再次使用【名片管理系统】') 84 break 85 else: 86 print('您的输入有误,请重新输入操作')