采用列表里套字典的方法,比较简单
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")
#分别进行不同的操作