import os
file_name = 'contact.txt'
def menu():
print("=====================欢迎使用通讯录=====================")
print("*********************菜单*****************************")
print("* 1.新建联系人 *")
print("* 2.查找联系人 *")
print("* 3.删除联系人 *")
print("* 4.修改联系人 *")
print("* 5.显示联系人信息 *")
print("* 0.退出 *")
print("=====================================================")
def main():
while True:
menu()
choice = int(input("请输入0-5选择:"))
if choice in [0, 1, 2, 3, 4, 5]:
if choice == 0:
print("谢谢使用!")
break
elif choice == 1:
insert()
elif choice == 2:
search()
elif choice == 3:
delete()
elif choice == 4:
modify()
elif choice == 5:
display()
def insert():
contact_list = []
while True:
name = input("请输入联系人姓名:")
if not name:
break
number = input("请输入联系人手机号:")
if not number:
break
try:
sex = input("请输入联系人性别:")
address = input("请输入联系人地址:")
qq = input("请输入联系人qq号:")
except:
print("输入错误,请重新输入")
continue
contact = {'name': name,'number': number, 'sex': sex,
'address': address, 'qq': qq}
contact_list.append(contact)
answer = input('是否继续添加?y/n\n')
if answer == 'y':
continue
else:
break
save(contact_list)
print("联系人输入完毕!")
def save(list):
try:
cont_txt = open(file_name, 'a', encoding='utf-8')
except:
cont_txt = open(file_name, 'w', encoding='utf-8')
for item in list:
cont_txt.write(str(item) + '\n')
cont_txt.close()
def search():
contact_query = []
while True:
number = ''
name = ''
if os.path.exists(file_name):
mode = input("按手机号查找请输入1,按姓名查找请输入2:")
if mode == '1':
number = input("请输入手机号:")
elif mode == '2':
name = input("请输入姓名:")
else:
print("===========输入错误,请重新输入==========")
search()
with open(file_name, 'r', encoding='utf-8') as rfile:
contact = rfile.readlines()
for item in contact:
d = dict(eval(item))
if number != '':
if d['number'] == number:
contact_query.append(d)
elif name != '':
if d['name'] == name:
contact_query.append(d)
display_contact(contact_query)
contact_query.clear()
answer = input('是否继续查询y/n\n')
if answer == 'y':
continue
else:
break
else:
print("暂未保存联系人信息")
return
def display_contact(list):
if len(list) == 0:
print("没有查询到联系人信息,无数据显示!!")
return
format_title = '{:^6}\t{:^12}\t{:^8}\t{:^10}'
print(format_title.format('姓名', '手机号', '性别', '地址', 'qq'))
format_data = '{:^6}\t{:^12}\t{:^8}\t{:^10}'
for item in list:
print(format_data.format(item.get('name'), item.get('number'),
item.get('sex'), item.get('address'),
item.get('qq')))
def delete():
while True:
contact_name = input(("请输入删除的联系人姓名:"))
if contact_name != ' ':
if os.path.exists(file_name):
with open(file_name, 'r', encoding='utf-8')as file:
contact_old = file.readlines()
else:
contact_old = []
flag = False
if contact_old:
with open(file_name, 'w', encoding='utf-8')as wfile:
d = {}
for item in contact_old:
d = dict(eval(item))
if d['name'] != contact_name:
wfile.write(str(d) + '\n')
else:
flag = True
if flag:
print(f'名字为{contact_name}的联系人已被删除')
else:
print(f'无联系人信息!')
else:
print('无联系人信息')
break
display()
answer = input('是否继续删除?y/n\n')
if answer == 'y':
continue
else:
break
def modify():
display()
if os.path.exists(file_name):
with open(file_name, 'r', encoding='utf-8') as rfile:
contact_old = rfile.readlines()
else:
return
contact_number = input("请输入要修改的联系人姓名:")
with open(file_name, 'w', encoding='utf-8')as wfile:
for item in contact_old:
d = dict(eval(item))
if d['name'] == contact_number:
print("找到该联系人!")
while True:
try:
d['number'] = input("请输入手机号:")
d['sex'] = input("请输入性别:")
d['address'] = input("请输入地址:")
d['qq'] = input("请输入qq:")
except:
print("您的输入有误,请重新输入!")
else:
break
wfile.write(str(d) + '\n')
print("修改成功!")
else:
wfile.write(str(d) + '\n')
answer = input('是否继续修改其他联系人信息y/n\n')
if answer == 'y':
modify()
def display():
contact_list = []
if os.path.exists(file_name):
with open(file_name, 'r', encoding='utf-8') as rfile:
contacts = rfile.readlines()
for item in contacts:
contact_list.append(eval(item))
if contact_list:
display_contact(contact_list)
else:
print('暂未保存过数据!!!')
main()
Python通讯录项目,请问运行时,显示全部联系人不显示QQ怎么回事
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- Feng-licong 2021-07-01 16:12关注
format_title = '{:^6}\t{:^12}\t{:^8}\t{:^10}'
你这里只有4个,所没发输出5个
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
- ¥20 怎么用dlib库的算法识别小麦病虫害
- ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
- ¥15 java写代码遇到问题,求帮助
- ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
- ¥15 有了解d3和topogram.js库的吗?有偿请教
- ¥100 任意维数的K均值聚类
- ¥15 stamps做sbas-insar,时序沉降图怎么画
- ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
- ¥15 关于#Java#的问题,如何解决?