栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python实现通讯录的增删改查功能

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python实现通讯录的增删改查功能

文件上输入通讯录的内容,包含编号、姓名、电话、地址、邮件、生日信息:

要求:

1.将数据提前录入到文本里 

2.提供基于人名的增、删、改、查功能。

3.电话号码允许多个

4.邮件、电话和生日(20001011)要进行格式的合法性判断

5.能够将修改后的内容保存到文件里。

print("-"*20+'ENTER ADDRESS BOOOK'+'-'*20)
print("Hello!Friends!")

print("""
***增添名单请按:1
***输出所有请按:2
***删除名单请按:3
***修改名单请按:4
***查找名单请按:5
***退出程序请按:6
""")
file = open('5.2.txt','r')
list_1=[]
for line in file:
    list_1.append(eval(line))
while 1:
    option = int(input('请输入你的选择:'))
    if option == 1:
        list_2 = {}
        number=len(list_1)+1
        name = input('请输入添加联系人的名字:')
        Telephone = input('请输入电话号码:')
        mailbox=input('请输入邮箱:')
        address = input('请输入住址:')
        birthday = input('请输入生日:')
        if str.isalpha(name) == 1:
            f=1
        else:
            print("输入不规范,请输入英文名")
            break
        if str.isdigit(Telephone) == 1:
            f=1
        else:
            print("输入不规范,请输入纯数字")
            break
        if str.isalpha(address) == 1:
            f=1
        else:
            print("输入不规范,请输入英文地址")
            break       
        list_2['num'] = number
        list_2['name'] = name
        list_2['Telephone'] = Telephone
        list_2['mailbox'] = mailbox
        list_2['address'] = address
        list_2['birthday'] = birthday
        list_1.append(list_2)
        print('添加成功!n')
        file1 = open('5.2.txt', 'at')
        for i in list_1:
            file1.write(str(i).strip())
            file1.write('n')
        file1.close()
    elif option == 2:
        for i in list_1:
            print(f'序号:{i["num"]}n姓名:{i["name"]}n电话号码:{i["Telephone"]}n邮箱:{i["mailbox"]}n住址:{i["address"]}n生日:{i["birthday"]} ')
            print('n')
    elif option == 3:
        name1 = input('请输入你要删除的联系人:')
        if str.isalpha(name1) == 1:
            f=1
        else:
            print("输入不规范,请输入英文名")
            break
        flag=1
        for j in list_1:
            if j['name'] == name1:
                list_1.remove(j)
                print('删除完成')
        for t in list_1:
            t['num'] = flag
            flag = flag+1
            print(t['num'])
        file3 = open('5.2.txt', 'w')
        for i in list_1:
            file3.write(str(i).strip())
            file3.write('n')
        file3.close()
    elif option == 4:
        name2 = input('请输入你要修改联系人的姓名:')
        if str.isalpha(name2) == 1:
            f=1
        else:
            print("输入不规范")
            break
        for k in list_1:
            if k['name'] == name2:
                new_name = input('请输入新的姓名:')
                new_Telephone = input('请输入新的电话号码:')
                new_mailbox = input('请输入新的邮箱:')
                new_address = input('请输入新的住址:')
                new_birthday = input('请输入新的生日:')
                if str.isalpha(new_name) == 1:
                    f = 1
                else:
                    print("输入不规范")
                    break
                if str.isdigit(new_Telephone) == 1:
                    f = 1
                else:
                    print("输入不规范")
                    break
                if type(new_mailbox) == str:
                    f = 1
                else:
                    print("输入不规范")
                    break
                if str.isalpha(new_address) == 1:
                    f = 1
                else:
                    print("输入不规范")
                    break
                if str.isdigit(new_Telephone) == 1:
                    print("正在添加")
                else:
                    print("输入不规范")
                    break
                k['name'] = new_name
                k['Telephone'] = new_Telephone
                k['mailbox'] = new_mailbox
                k['address'] = new_address
                k['birthday'] = new_birthday
                print('修改成功')
        file4 = open('5.2.txt', 'w')
        print(list_1)
        for i in list_1:
            file4.write(str(i).strip())
            file4.write('n')
        file4.close()
    elif option == 5:
        name3 = input('请输入你要查找的联系人姓名:')
        if str.isalpha(name3) == 1:
            f=1
        else:
            print("输入不规范")
            break
        for l in list_1:
            if l['name'] == name3:
                print(f'姓名:{l["name"]}n电话号码:{l["Telephone"]}n邮箱:{l["mailbox"]}n住址:{l["address"]}  ')
    elif option == 6:
        break
    else:
        print('当前没有该选项,请重新输入')

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/354716.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号