Python:用字典建立一个通讯录,向字典中添加和删除通讯人信息,查询某个人的信息,然后输出通讯录中所有人的信息。

用字典建立一个通讯录,向字典中添加和删除通讯人(名字、电话、邮箱、工作单位等),查询某个人的信息,然后输出通讯录中所有人的信息。

采用列表里套字典的方法,比较简单

contacts = []

def add():
    mes = "Please enter the message of the people,and spilt them with space"
    mes +="\nphone email adress "
    name = input("Please enter the name of the people:\n")
    #定义提示信息,收集名字进行下一步的判断
    if name in contacts:
        print(f"{name.title()} is insist,whether change the people's information,")
        com =("Please enter 'yes' or 'no'")
        #如果已经收录此人信息,反馈以获得下一步的行动
        email = 'NONE'
        adress = 'NONE'
        if com == 'yes':
            contact.remove(name)
            #将原信息删除
            try:
                phone,email,address = input(mes).split()
                contacts.append({'name' : name , 'phone' : phone , 'email' : email,'address':address})
            except ValueError:
                print("The message is not enough")
                #收集三个信息,并且在信息不足时反馈给用户
    else:
        try:
            phone,email,address = input(mes).split()
            contacts.append({'name' : name , 'phone' : phone , 'email' : email,'address':address})
        except ValueError:
                print("The message is not enough")
                #在此用户不存在时进行正常流程

def lookfor(name):
    for i in contacts:
        if i['name'] == name:
            return i
        else:
            print("The people doesn't in your contacts")
            #定义查找函数,返回需要查找的联系人的字典

def delect():
    name = input("Please input the name you want to delect")
    contacts.remove(lookfor(name))
    #进行删除联系人

def inquire():
    name = input("Please enter the name you want to inquire")
    print(lookfor(name))
    #输出查找联系人的信息

def outputall():
    for i in contacts:
        print(i)
#输出全部信息
while True:
    comand = "\nYou can enter different command to realize different functions,\n"
    comand +="1 . add liaisons;\n2 . delect liaisons; \n3 .inquire liaisons ; \n"
    comand +="4.out put all over information.\nYou can enter 'q' at any time to quit"
    print(comand)#给予操作提示
    cmd = input("Please enter the command you want to realize:\n")
    #收集操作指令
    if cmd == '1':
        add()
    elif cmd == '2':
        delect()
    elif cmd == '3':
        inquire()
    elif cmd == '4':
        outputall()
    elif cmd == '5':
        break
    else:
        print("\nThe command is not allowed")
#分别进行不同的操作

  • 4
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值