python开发联系人管理系统完整代码
时间: 2023-02-09 12:00:28 浏览: 97
这是一个使用 Python 开发的联系人管理系统的完整代码示例:
```
# 导入必要的模块
import os
import sys
# 定义联系人类
class Contact:
def __init__(self, name, phone, email):
self.name = name
self.phone = phone
self.email = email
# 定义联系人列表类
class ContactList:
def __init__(self):
self.contacts = []
# 添加联系人
def add_contact(self, contact):
self.contacts.append(contact)
# 查找联系人
def find_contact(self, name):
for contact in self.contacts:
if contact.name == name:
return contact
return None
# 删除联系人
def delete_contact(self, name):
contact = self.find_contact(name)
if contact:
self.contacts.remove(contact)
# 定义主函数
def main():
# 创建联系人列表对象
contact_list = ContactList()
# 循环处理用户输入的命令
while True:
# 显示菜单
print("1. 添加联系人")
print("2. 查找联系人")
print("3. 删除联系人")
print("4. 退出系统")
print()
try:
choice = int(input("请输入你的选择: "))
except ValueError:
print("输入无效,请重试。")
continue
# 根据用户输入执行相应操作
if choice == 1:
# 添加联系人
name = input("请输入联系人姓名: ")
phone = input("请输入联系人电话: ")
email = input("请输入联系人电子邮件: ")
contact = Contact(name, phone, email)
contact_list.add_contact(contact)
print("联系人添加成功。")
elif choice == 2:
# 查