温馨提示×

python怎么实现简单通讯录管理系统

小亿
108
2023-08-18 09:53:31
栏目: 编程语言

要实现简单的通讯录管理系统,可以使用Python的字典来存储联系人的信息。以下是一个简单的实现示例:

contacts = {}
def add_contact():
name = input("请输入联系人姓名:")
phone = input("请输入联系人电话:")
email = input("请输入联系人邮箱:")
contacts[name] = {"电话": phone, "邮箱": email}
print("联系人添加成功!")
def search_contact():
name = input("请输入要查询的联系人姓名:")
if name in contacts:
print("姓名:", name)
print("电话:", contacts[name]["电话"])
print("邮箱:", contacts[name]["邮箱"])
else:
print("未找到该联系人!")
def delete_contact():
name = input("请输入要删除的联系人姓名:")
if name in contacts:
del contacts[name]
print("联系人删除成功!")
else:
print("未找到该联系人!")
def list_contacts():
print("所有联系人:")
for name in contacts:
print("姓名:", name)
print("电话:", contacts[name]["电话"])
print("邮箱:", contacts[name]["邮箱"])
print("===================")
def main():
while True:
print("1. 添加联系人")
print("2. 查询联系人")
print("3. 删除联系人")
print("4. 显示所有联系人")
print("5. 退出")
choice = input("请输入操作编号:")
if choice == "1":
add_contact()
elif choice == "2":
search_contact()
elif choice == "3":
delete_contact()
elif choice == "4":
list_contacts()
elif choice == "5":
print("退出通讯录管理系统!")
break
else:
print("无效的操作编号!")
if __name__ == "__main__":
main()

以上代码实现了一个简单的通讯录管理系统,可以添加联系人、查询联系人、删除联系人和显示所有联系人。运行程序后,根据提示输入相应的操作编号即可进行相应的操作。

0