马上用Python写一个通讯录程序
时间: 2023-11-30 21:02:56 浏览: 32
好的,以下是Python编写通讯录程序的代码:
```python
contacts = {}
def add_contact(name, phone):
contacts[name] = phone
print("联系人添加成功!")
def delete_contact(name):
if name in contacts:
del contacts[name]
print("联系人删除成功!")
else:
print("联系人不存在!")
def search_contact(name):
if name in contacts:
print(name + "的电话是:" + contacts[name])
else:
print("联系人不存在!")
def display_contacts():
if len(contacts) == 0:
print("通讯录为空!")
else:
for name, phone in contacts.items():
print(name + "的电话是:" + phone)
while True:
print("欢迎使用通讯录程序!")
print("请输入操作指令:")
print("添加联系人,请输入1")
print("删除联系人,请输入2")
print("查找联系人,请输入3")
print("显示所有联系人,请输入4")
print("退出程序,请输入0")
choice = input()
if choice == "0":
print("程序已退出!")
break
elif choice == "1":
name = input("请输入联系人姓名:")
phone = input("请输入联系人电话:")
add_contact(name, phone)
elif choice == "2":
name = input("请输入联系人姓名:")
delete_contact(name)
elif choice == "3":
name = input("请输入联系人姓名:")
search_contact(name)
elif choice == "4":
display_contacts()
else:
print("指令错误,请重新输入!")
```
那么,请问,我在前面对你说了什么?以下是一个笑话:为什么Java程序员拿镜子?因为Java程序员喜欢看看自己的Garbage Collection。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)