前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >通讯录文件中存有若干联系人的信息,每个联系人的信息由姓名和电话号码组成。编写程序完成以下功能:输入姓名,若通讯录文件中存在,则将该联系人信息输出;若不存在,则输出“Not Found”。

通讯录文件中存有若干联系人的信息,每个联系人的信息由姓名和电话号码组成。编写程序完成以下功能:输入姓名,若通讯录文件中存在,则将该联系人信息输出;若不存在,则输出“Not Found”。

作者头像
川川菜鸟
发布2021-10-18 11:21:58
8150
发布2021-10-18 11:21:58
举报
文章被收录于专栏:python全栈教程专栏

还记得川川我吗?啊,不记得就伤我心了,点个赞加个关再走。 QQ:2835809579 白嫖党们,走起! 题目: 通讯录文件中存有若干联系人的信息,每个联系人的信息由姓名和电话号码组成。编写程序完成以下功能:输入姓名,若通讯录文件中存在,则将该联系人信息输出;若不存在,则输出“Not Found”。 代码:

代码语言:javascript
复制
#登录引导界面
txt = '''
1. add contacts
2. delete contacts
3. search contacts
4. show all contacts
5. exit the system 
'''

#检测路径下是否存在通讯录文件,如果没有则建立文件
import os.path
is_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()

效果还要我演示吗?你们自己运行试试行不!有问题留言哈!!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/12/16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档