通讯录系统Python代码
时间: 2023-06-27 14:03:34 浏览: 44
以下是一个简单的通讯录系统Python代码,可以实现添加、查找、删除和列出联系人的功能:
```python
contacts = {}
def add_contact(name, phone):
contacts[name] = phone
print("联系人已添加。")
def find_contact(name):
if name in contacts:
print(name + ": " + contacts[name])
else:
print("找不到该联系人。")
def delete_contact(name):
if name in contacts:
del contacts[name]
print("联系人已删除。")
else:
print("找不到该联系人。")
def list_contacts():
print("所有联系人:")
for name, phone in contacts.items():
print(name + ": " + phone)
while True:
print("请选择操作:")
print("1. 添加联系人")
print("2. 查找联系人")
print("3. 删除联系人")
print("4. 列出所有联系人")
print("5. 退出程序")
choice = input()
if choice == "1":
name = input("请输入联系人姓名:")
phone = input("请输入联系人电话:")
add_contact(name, phone)
elif choice == "2":
name = input("请输入联系人姓名:")
find_contact(name)
elif choice == "3":
name = input("请输入联系人姓名:")
delete_contact(name)
elif choice == "4":
list_contacts()
elif choice == "5":
break
else:
print("无效的选择,请重试。")
```
这个代码实现了一个基本的命令行界面,可以通过输入数字选择不同的操作。通过调用 `add_contact`、`find_contact`、`delete_contact` 和 `list_contacts` 函数来执行不同的操作。所有的联系人信息都保存在 `contacts` 字典中。