通讯录文件中存有若干联系人的信息,每个联系人的信息由姓名和电话号码组成。编写程序完成以下功能:输入姓名,若通讯录文件中存在,则将该联系人信息输出;若不存在,则输出“Not Found”。
发布日期:2021-06-29 14:38:20
浏览次数:2
分类:技术文章
本文共 2615 字,大约阅读时间需要 8 分钟。
还记得川川我吗?啊,不记得就伤我心了,点个赞加个关再走。
QQ:2835809579 白嫖党们,走起! 题目: 通讯录文件中存有若干联系人的信息,每个联系人的信息由姓名和电话号码组成。编写程序完成以下功能:输入姓名,若通讯录文件中存在,则将该联系人信息输出;若不存在,则输出“Not Found”。 代码:#登录引导界面txt = '''1. add contacts2. delete contacts3. search contacts4. show all contacts5. exit the system '''#检测路径下是否存在通讯录文件,如果没有则建立文件import os.pathis_exist = os.path.isfile('addressbook.txt')if is_exist == 0: new_file = open('Contacts.txt', 'w') new_file.close()#入口程序def start(): #设置循环,当用户输入特定选项退出 while True: print("Welcome, select a number:") print(txt) userchoice = int(input()) #输入错误序号则重启程序 if userchoice not in [1,2,3,4,5]: print('wrong choice') start() break #输入正确序号执行相应程序 elif userchoice == 1: add_contacts() elif userchoice == 2: delete_contacts() elif userchoice == 3: search_contacts() elif userchoice == 4: show_all_contacts() elif userchoice == 5: break#添加联系人def add_contacts(): print('Add new contacts') print('Name: ', end = '') Name = input() print('Sex: ', end = '') Sex = input() print('Relationship(Friend/ Family/ Classmate): ', end = '') Relationship = input() print('Number: ', end = '') Number = input() #将通讯录追加到文件末端 Contacts_file = open('Contacts.txt','a') Contacts_file.write(Name+'\t'+Sex+'\t'+Relationship+'\t'+Number+'\n') Contacts_file.close()#删除通讯录中的信息def delete_contacts(): print('Enter the name: ', end = '') name = input() Contacts_file = open('Contacts.txt', 'r') Contacts_list = [] #将通讯录缓存到列表内,遇到需要删除的通讯录条目则跳过 for line in Contacts_file.readlines(): if line.find(name) != -1: continue Contacts_list.append(line) #将通讯录清空,将缓存在列表中的通讯录信息加载进文件内 Contacts_file = open('Contacts.txt', 'w') for i in range(0, len(Contacts_list)): Contacts_file.write(Contacts_list[i]) Contacts_file.close()#搜索通讯录def search_contacts(): print('Enter the name: ',end = '') Search_name = input() Contacts_file = open('addressbook.txt','r',encoding='utf-8') for line in Contacts_file.readlines(): String = line find_or_not = String.find(Search_name) if find_or_not !=-1 : print(line) break #若搜索不到,返回Not Found! if find_or_not == -1: print('Not Found!') Contacts_file.close()#显示通讯录所有条目def show_all_contacts(): print('Name\tSex\tRelationship\tNumber', end = '\n') Contacts_file = open('addressbook.txt','r') print(Contacts_file.read()) Contacts_file.close()#执行入口程序start()
效果还要我演示吗?你们自己运行试试行不!有问题留言哈!!
转载地址:https://chuanchuan.blog.csdn.net/article/details/111244520 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月20日 09时21分28秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
猿来绘Java-46-Collection接口及其方法
2019-04-29
猿来绘Java-47- foreatch 增强for循环
2019-04-29
2021/4/27课堂总结和作业
2019-04-29
2021.4.28课堂总结和作业
2019-04-29
2021.4.29课堂总结
2019-04-29
2021.4.30课堂总结和作业
2019-04-29
需要吗?2000GB+学习视频教程 面试资料免费下载
2019-04-29
MySQL对已存在数据库表添加自增ID字段
2019-04-29
idea中的一些常用快捷键
2019-04-29
js校验表单后提交表单的三种方法总结【转载】
2019-04-29
欢迎使用CSDN-markdown编辑器
2019-04-29
a标签中href调用js的几种方法
2019-04-29
jstl标签详解
2019-04-29
Eclipse中使用SVN的使用
2019-04-29
JSON.parse和eval的区别
2019-04-29
JQuery中$.ajax()方法参数详解
2019-04-29
正则表达式的数字实例
2019-04-29
【转】EasyUI 验证
2019-04-29