Python:用字典建立一个通讯录,向字典中添加和删除通讯人信息,查询某个人的信息,然后输出通讯录中所有人的信息。
生活随笔
收集整理的這篇文章主要介紹了
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 ielse: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':breakelse:print("\nThe command is not allowed") #分別進行不同的操作總結
以上是生活随笔為你收集整理的Python:用字典建立一个通讯录,向字典中添加和删除通讯人信息,查询某个人的信息,然后输出通讯录中所有人的信息。的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 编写函数,模拟内置函数 sorted()
- 下一篇: Python:用生成器的方式计算任意起止