一、先搭建一个框架。有条理地把要实现的功能罗列出来!
# 定义一个字典用于暂存信息(程序结束,数据删除)contacts = dict()input_buf = '请输入序号:'while True:# 打印菜单界面print(f''' {"="*8}手机通讯录{"="*8}1.添加联系人\t\t2.查看联系人3.删除联系人\t\t4.修改联系人5.查找联系人\t\t0.退出''')# 选择菜单choice_menu = input(input_buf)input_buf = '请输入序号:'# 防止输入非范围内的字符if choice_menu not in tuple('012345'):input_buf = '请重新输入序号:'continue# 1.添加联系人 ============================elif choice_menu == '1':print(1)# 2.查看联系人 ============================elif choice_menu == '2':print(2)# 3.删除联系人 ============================elif choice_menu == '3':print(3)# 4.修改联系人 ============================elif choice_menu == '4':print(4)# 5.查找联系人 ============================elif choice_menu == '5':print(5)# 0.退出============================else:print(f'{"="*6}已退出手机通讯录{"="*6}')break
运行结果:
========手机通讯录========1.添加联系人2.查看联系人3.删除联系人4.修改联系人5.查找联系人0.退出请输入序号:11 ========手机通讯录========1.添加联系人2.查看联系人3.删除联系人4.修改联系人5.查找联系人0.退出请输入序号:9 ========手机通讯录========1.添加联系人2.查看联系人3.删除联系人4.修改联系人5.查找联系人0.退出请重新输入序号:a ========手机通讯录========1.添加联系人2.查看联系人3.删除联系人4.修改联系人5.查找联系人0.退出请重新输入序号:0======已退出手机通讯录======
二、有序实现具体功能。根据项目要求编写代码
1.添加联系人
# 1.添加联系人 ============================elif choice_menu == '1':print(f' {"=" * 8}添加联系人{"=" * 8}')# 防止输入空字符和首字符空格' '(无法防止多个空格输入)while True:name = input('请输入名字:')if name != '' and name != ' ':breakwhile True:phone = input('请输入手机号码:')if phone != '' and phone != ' ':breakwhile True:email = input('请输入邮箱:')if email != '' and email != ' ':breakwhile True:address = input('请输入地址:')if address != '' and address != ' ':breakkeys_name += 1# 数据写入字典contacts[keys_name] = {'name': name, 'phone': phone, 'email': email, 'address': address}# print(contacts)print('已保存成功')
2.查看联系人
# 2.查看联系人 ============================elif choice_menu == '2':print(f' {"=" * 8}查看联系人{"=" * 8}')# 是否为空字典if contacts == dict():print('哦豁,此手机通讯录无数据!!!')continue# 遍历打印通讯录for i in contacts.values():# print(i)print(f"姓名:{i['name']}\t手机号码:{i['phone']}\t邮箱:{i['email']}\t住址:{i['address']}")
3.删除联系人
# 3.删除联系人 ============================elif choice_menu == '3':print(f' {"=" * 8}删除联系人{"=" * 8}')# 是否为空字典if contacts == dict():print('哦豁,此手机通讯录无数据!!!')continuename = input('请输入需要删除的名字:')name_count = 0# 记录重名的次数keys_buf = list() # 存储重名的键值,方便字典索引# 遍历查找此人for i in contacts:if contacts[i]['name'] == name:name_count += 1 # 重名计数keys_buf.append(i)# 把找到名字的字典键值追加到列表中print(f"{name_count}.姓名:{contacts[i]['name']}\t"f"手机号码:{contacts[i]['phone']}\t"f"邮箱:{contacts[i]['email']}\t"f"住址:{contacts[i]['address']}")# 多个重名选择删除if name_count > 1:name_choice = int(input('请选择需要删除名字的序号:'))for i in range(name_count):if (name_choice-1) == i:contacts.pop(keys_buf[i]) # 删除字典元素print('已删除成功!')# 只有一个删除elif name_count == 1:contacts.pop(keys_buf[0]) # 删除字典元素print('已删除成功!')# 找不到此人else:print('此人不在通讯录,请确认输入是否有误!')
4.修改联系人
# 4.修改联系人 ============================elif choice_menu == '4':print(f' {"=" * 8}修改联系人{"=" * 8}')# 是否为空字典if contacts == dict():print('哦豁,此手机通讯录无数据!!!')continuename = input('请输入需要修改的名字:')name_count = 0# 记录重名的次数keys_buf = list() # 存储重名的键值,方便字典索引# 遍历查找此人for i in contacts:if contacts[i]['name'] == name:name_count += 1 # 重名计数keys_buf.append(i)# 把找到名字的字典键值追加到列表中print(f"{name_count}.姓名:{contacts[i]['name']}\t"f"手机号码:{contacts[i]['phone']}\t"f"邮箱:{contacts[i]['email']}\t"f"住址:{contacts[i]['address']}")# 多个重名选择修改if name_count > 1:name_choice = int(input('请选择需要修改名字的序号:'))for i in range(name_count):if (name_choice - 1) == i:# 防止输入空字符和首字符空格' '(无法防止多个空格输入)while True:new_name = input('请输入新的名字:')if new_name != '' and new_name != ' ':breakwhile True:new_phone = input('请输入新的手机号码:')if new_phone != '' and new_phone != ' ':breakwhile True:new_email = input('请输入新的邮箱:')if new_email != '' and new_email != ' ':breakwhile True:new_address = input('请输入新的地址:')if new_address != '' and new_address != ' ':break# 新数据覆盖字典contacts[keys_buf[i]] = {'name': new_name, 'phone': new_phone, 'email': new_email, 'address': new_address}# print(contacts)print('已修改成功')# 只有一个删除elif name_count == 1:# 防止输入空字符和首字符空格' '(无法防止多个空格输入)while True:new_name = input('请输入新的名字:')if new_name != '' and new_name != ' ':breakwhile True:new_phone = input('请输入新的手机号码:')if new_phone != '' and new_phone != ' ':breakwhile True:new_email = input('请输入新的邮箱:')if new_email != '' and new_email != ' ':breakwhile True:new_address = input('请输入新的地址:')if new_address != '' and new_address != ' ':break# 数据覆盖字典contacts[keys_buf[0]] = {'name': new_name, 'phone': new_phone, 'email': new_email, 'address': new_address}# print(contacts)print('已修改成功')# 找不到此人else:print('此人不在通讯录,请确认输入是否有误!')
5.查找联系人
# 5.查找联系人 ============================elif choice_menu == '5':print(f' {"=" * 8}查找联系人{"=" * 8}')# 是否为空字典if contacts == dict():print('哦豁,此手机通讯录无数据!!!')continuename = input('请输入需要查找的名字:')name_count = 0# 记录重名的次数# 遍历查找此人for i in contacts:if contacts[i]['name'] == name:name_count += 1 # 重名计数print(f"{name_count}.姓名:{contacts[i]['name']}\t"f"手机号码:{contacts[i]['phone']}\t"f"邮箱:{contacts[i]['email']}\t"f"住址:{contacts[i]['address']}")# 找不到此人if name_count == 0:print('此人不在通讯录,请确认输入是否有误!')
三、代码汇总。实现完整的功能
"""用字典模拟手机通讯录知识点:1、嵌套字典使用2、字典增删改查3、嵌套循环/条件语句/跳转语句4、数据类型转换/元素索引改善与不足:1、代码块很臃肿,有很多重复的代码,如果使用函数就能避免2、在多个重名选择输入序号时,没有做限制3、程序结束,数据删除,无法保存"""# 定义一个字典用于暂存信息(程序结束,数据删除)contacts = dict()input_buf = '请输入序号:'keys_name = 0 # 字典键值命名# 功能实现===========================================================while True:# 打印菜单界面print(f''' {"=" * 8}手机通讯录{"=" * 8}1.添加联系人\t\t2.查看联系人3.删除联系人\t\t4.修改联系人5.查找联系人\t\t0.退出''')# 选择菜单choice_menu = input(input_buf)input_buf = '请输入序号:'# 防止输入非范围内的字符if choice_menu not in tuple('012345'):input_buf = '请重新输入序号:'continue# 1.添加联系人 ====================================================elif choice_menu == '1':print(f' {"=" * 8}添加联系人{"=" * 8}')# 防止输入空字符和首字符空格' '(无法防止多个空格输入)while True:name = input('请输入名字:')if name != '' and name != ' ':breakwhile True:phone = input('请输入手机号码:')if phone != '' and phone != ' ':breakwhile True:email = input('请输入邮箱:')if email != '' and email != ' ':breakwhile True:address = input('请输入地址:')if address != '' and address != ' ':breakkeys_name += 1# 数据写入字典contacts[keys_name] = {'name': name, 'phone': phone, 'email': email, 'address': address}# print(contacts)print('已保存成功')# 2.查看联系人 ====================================================elif choice_menu == '2':print(f' {"=" * 8}查看联系人{"=" * 8}')# 是否为空字典if contacts == dict():print('哦豁,此手机通讯录无数据!!!')continue# 遍历打印通讯录for i in contacts.values():# print(i)print(f"姓名:{i['name']}\t手机号码:{i['phone']}\t邮箱:{i['email']}\t住址:{i['address']}")# 3.删除联系人 ====================================================elif choice_menu == '3':print(f' {"=" * 8}删除联系人{"=" * 8}')# 是否为空字典if contacts == dict():print('哦豁,此手机通讯录无数据!!!')continuename = input('请输入需要删除的名字:')name_count = 0# 记录重名的次数keys_buf = list()# 存储重名的键值,方便字典索引# 遍历查找此人for i in contacts:if contacts[i]['name'] == name:name_count += 1# 重名计数keys_buf.append(i)# 把找到名字的字典键值追加到列表中print(f"{name_count}.姓名:{contacts[i]['name']}\t"f"手机号码:{contacts[i]['phone']}\t"f"邮箱:{contacts[i]['email']}\t"f"住址:{contacts[i]['address']}")# 多个重名选择删除if name_count > 1:name_choice = int(input('请选择需要删除名字的序号:'))for i in range(name_count):if (name_choice - 1) == i:contacts.pop(keys_buf[i])# 删除字典元素print('已删除成功!')# 只有一个删除elif name_count == 1:contacts.pop(keys_buf[0])# 删除字典元素print('已删除成功!')# 找不到此人else:print('此人不在通讯录,请确认输入是否有误!')# 4.修改联系人 ====================================================elif choice_menu == '4':print(f' {"=" * 8}修改联系人{"=" * 8}')# 是否为空字典if contacts == dict():print('哦豁,此手机通讯录无数据!!!')continuename = input('请输入需要修改的名字:')name_count = 0# 记录重名的次数keys_buf = list() # 存储重名的键值,方便字典索引# 遍历查找此人for i in contacts:if contacts[i]['name'] == name:name_count += 1 # 重名计数keys_buf.append(i)# 把找到名字的字典键值追加到列表中print(f"{name_count}.姓名:{contacts[i]['name']}\t"f"手机号码:{contacts[i]['phone']}\t"f"邮箱:{contacts[i]['email']}\t"f"住址:{contacts[i]['address']}")# 多个重名选择修改if name_count > 1:name_choice = int(input('请选择需要修改名字的序号:'))for i in range(name_count):if (name_choice - 1) == i:# 防止输入空字符和首字符空格' '(无法防止多个空格输入)while True:new_name = input('请输入新的名字:')if new_name != '' and new_name != ' ':breakwhile True:new_phone = input('请输入新的手机号码:')if new_phone != '' and new_phone != ' ':breakwhile True:new_email = input('请输入新的邮箱:')if new_email != '' and new_email != ' ':breakwhile True:new_address = input('请输入新的地址:')if new_address != '' and new_address != ' ':break# 新数据覆盖字典contacts[keys_buf[i]] = {'name': new_name, 'phone': new_phone, 'email': new_email, 'address': new_address}# print(contacts)print('已修改成功')# 只有一个删除elif name_count == 1:# 防止输入空字符和首字符空格' '(无法防止多个空格输入)while True:new_name = input('请输入新的名字:')if new_name != '' and new_name != ' ':breakwhile True:new_phone = input('请输入新的手机号码:')if new_phone != '' and new_phone != ' ':breakwhile True:new_email = input('请输入新的邮箱:')if new_email != '' and new_email != ' ':breakwhile True:new_address = input('请输入新的地址:')if new_address != '' and new_address != ' ':break# 数据覆盖字典contacts[keys_buf[0]] = {'name': new_name, 'phone': new_phone, 'email': new_email, 'address': new_address}# print(contacts)print('已修改成功')# 找不到此人else:print('此人不在通讯录,请确认输入是否有误!')# 5.查找联系人 ====================================================elif choice_menu == '5':print(f' {"=" * 8}查找联系人{"=" * 8}')# 是否为空字典if contacts == dict():print('哦豁,此手机通讯录无数据!!!')continuename = input('请输入需要查找的名字:')name_count = 0# 记录重名的次数# 遍历查找此人for i in contacts:if contacts[i]['name'] == name:name_count += 1 # 重名计数print(f"{name_count}.姓名:{contacts[i]['name']}\t"f"手机号码:{contacts[i]['phone']}\t"f"邮箱:{contacts[i]['email']}\t"f"住址:{contacts[i]['address']}")# 找不到此人if name_count == 0:print('此人不在通讯录,请确认输入是否有误!')# 0.退出==========================================================else:print(f'{"=" * 6}已退出手机通讯录{"=" * 6}')break
运行结果:
========手机通讯录========1.添加联系人2.查看联系人3.删除联系人4.修改联系人5.查找联系人0.退出请输入序号:5 ========查找联系人========哦豁,此手机通讯录无数据!!! ========手机通讯录========1.添加联系人2.查看联系人3.删除联系人4.修改联系人5.查找联系人0.退出请输入序号:1 ========添加联系人========请输入名字:周华请输入手机号码:1008611请输入邮箱:123.com请输入地址:广州已保存成功 ========手机通讯录========1.添加联系人2.查看联系人3.删除联系人4.修改联系人5.查找联系人0.退出请输入序号:2 ========查看联系人========姓名:周华手机号码:1008611邮箱:123.com住址:广州
作者:周华
创作日期:2023/9/27