Python 通讯录代码
时间: 2023-12-25 22:29:38 浏览: 54
以下是一个简单的Python通讯录程序的代码示例:
```python
# 创建一个空的通讯录字典
contacts = {}
# 添加联系人
def add_contact():
name = input("请输入联系人姓名:")
phone = input("请输入联系人电话:")
contacts[name] = phone
print("联系人添加成功!")
# 查找联系人
def search_contact():
name = input("请输入要查找的联系人姓名:")
if name in contacts:
print("联系人电话:", contacts[name])
else:
print("联系人不存在!")
# 删除联系人
def delete_contact():
name = input("请输入要删除的联系人姓名:")
if name in contacts:
del contacts[name]
print("联系人删除成功!")
else:
print("联系人不存在!")
# 显示所有联系人
def show_all_contacts():
if len(contacts) == 0:
print("通讯录为空!")
else:
print("所有联系人:")
for name, phone in contacts.items():
print(name, ":", phone)
# 退出通讯录
def exit_program():
print("感谢使用通讯录程序!")
print("\n")
# 主程序循环
while True:
print("|-- 通讯录程序 --|")
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':
show_all_contacts()
elif choice == '5':
exit_program()
break
else:
print("输入无效,请重新输入!")
```