python好友管理系统代码
时间: 2023-09-29 10:08:07 浏览: 104
以下是一个基于Python的简单好友管理系统的代码示例:
```python
friends = []
def add_friend():
name = input("请输入好友的姓名:")
age = input("请输入好友的年龄:")
gender = input("请输入好友的性别:")
friend = {"name": name, "age": age, "gender": gender}
friends.append(friend)
print("已成功添加好友:", friend)
def show_friends():
for friend in friends:
print("姓名:", friend["name"])
print("年龄:", friend["age"])
print("性别:", friend["gender"])
print("------------------")
def delete_friend():
name = input("请输入要删除的好友姓名:")
for friend in friends:
if friend["name"] == name:
friends.remove(friend)
print("已成功删除好友:", friend)
break
else:
print("没有找到该好友:", name)
while True:
print("请选择操作:")
print("1. 添加好友")
print("2. 显示好友")
print("3. 删除好友")
print("4. 退出程序")
choice = input()
if choice == "1":
add_friend()
elif choice == "2":
show_friends()
elif choice == "3":
delete_friend()
elif choice == "4":
print("程序已退出,谢谢使用!")
break
else:
print("无效的选择,请重新输入!")
```
该程序通过一个friends列表来存储好友信息,用户可以选择添加、显示或删除好友,并在程序退出时保存好友信息。需要注意的是,该代码示例并不完整,如果需要实际应用,还需要添加数据持久化、输入校验等功能。