2024年4月16日发(作者:)
python好友管理系统代码
Python好友管理系统代码
介绍
Python是一种高级编程语言,广泛应用于各种领域,包括Web开发、
数据科学、人工智能等。本文将介绍如何使用Python编写一个好友
管理系统,该系统可以实现添加、删除、查找和显示好友列表等功能。
环境
在开始之前,请确保您已经安装了Python,并且已经配置好了相关的
开发环境。本文使用的是Python 3.7版本。
代码实现
1. 导入模块
首先,我们需要导入相关的模块。在本例中,我们需要使用os模块来
处理文件操作,并且需要使用json模块来处理JSON格式的数据。
import os
import json
2. 定义变量和函数
接下来,我们可以定义一些变量和函数。在本例中,我们需要定义一
个变量来存储好友列表,并且需要定义一些函数来实现添加、删除、
查找和显示好友列表等功能。
# 定义变量
friends = []
# 定义函数
def add_friend():
name = input("请输入姓名:")
age = input("请输入年龄:")
gender = input("请输入性别:")
friend = {"name": name, "age": age, "gender": gender}
(friend)
save_friends()
def delete_friend():
name = input("请输入要删除的姓名:")
for friend in friends:
if friend["name"] == name:
(friend)
save_friends()
break
def search_friend():
name = input("请输入要查找的姓名:")
for friend in friends:
if friend["name"] == name:
print(friend)
break
else:
print("未找到该好友。")
def show_friends():
for friend in friends:
print(friend)
def save_friends():
with open("", "w") as f:
(friends, f)
def load_friends():
global friends
if (""):
with open("", "r") as f:
friends = (f)
3. 实现主程序
最后,我们可以实现主程序。在本例中,我们可以使用一个无限循环
来实现用户与系统的交互。
if __name__ == "__main__":
load_friends()
while True:
print("""
请选择操作:
1. 添加好友
2. 删除好友
3. 查找好友
4. 显示好友列表
5. 退出程序""")
choice = input()
if choice == "1":
add_friend()
elif choice == "2":
delete_friend()
elif choice == "3":
search_friend()
elif choice == "4":
show_friends()
elif choice == "5":
break
else:
print("输入有误,请重新输入。")
4. 总结
通过上述代码实现,我们可以编写一个简单的Python好友管理系统,
该系统可以实现添加、删除、查找和显示好友列表等功能。当然,这
只是一个简单的示例,您可以根据自己的需要进行修改和扩展。
发布者:admin,转转请注明出处:http://www.yc00.com/web/1713226101a2207277.html
评论列表(0条)