一、需求分析
- 显示所有学生信息
- 新建学生信息
- 查询学生信息
- 修改学生信息
- 删除学生信息
- 选择显示功能
- 打印平均分
- 打印及格率
- 退出操作
- 退出系统
二、Python文件
import functools
import os
# 存储学生信息
# stu_mesall = {
# "s001": {"sno": "s001", "name": "张三", "age": 23, "sex": "男", "Python": 45},
# "s002": {"sno": "s002", "name": "李思", "age": 18, "sex": "女", "Python": 87}
# }
#
# with open("学生信息.txt", mode="w", encoding="utf-8") as stu_mesalls:
# stu_mesalls.write(str(stu_mesall))
stu_mesall = {}
# 显示所有学生信息
def show_mes():
print("""
*********************************************
欢迎使用【不染学员管理信息系统】V2.0
1\. 显示所有学生信息
2\. 新建学生信息
3\. 查询学生信息
4\. 修改学生信息
5\. 删除学生信息
6\. 选择显示功能
7\. 退出系统
*********************************************
""")
# 显示所有学生信息
def show_all():
if os.path.exists("学生信息.txt"):
with open("学生信息.txt", mode="r", encoding="utf-8") as file:
ret = file.read()
global stu_mesall
stu_mesall = eval(ret)
for item in stu_mesall.values():
print(f"学号:{item['sno']} | 姓名:{item['name']} | 年龄:{item['age']} | 性别:{item['sex']} | Python成绩:{item['Python']}")
# 新建学生信息
def add_stu():
stu_sno = input("请输入您要新建学生的学号:")
if stu_sno in stu_mesall.keys():
print("""
*********************************************
您输入的学生学号已存在,请输入以下指令:
a. 重新输入学号
0\. 回到主菜单
*********************************************
""")
while True:
info_help = input("请输入您要操作的指令:")
if info_help == 'a':
add_stu()
elif info_help == '0':
return
else:
print("输入有误,请重新输入!")
else:
stu_name = input("请输入您要新建学生的姓名:")
stu_age = int(input("请输入您要新建学生的年龄:"))
stu_sex = input("请输入您要新建学生的性别:")
stu_py = int(input("请输入您要新建学生的Python成绩:"))
stu_mes = {"sno": stu_sno, "name": stu_name, "age": stu_age, "sex": stu_sex, "Python": stu_py}
stu_mesall[stu_sno] = stu_mes
with open("学生信息.txt", mode="w", encoding="utf-8") as stu_mesalls:
stu_mesalls.write(str(stu_mesall))
print("添加成功!")
return
# 查询学生信息
def sel_stu():
stu_sno = input("请输入您要查询学生的学号:")
if stu_sno in stu_mesall.keys():
print(f"学号:{stu_mesall[stu_sno]['sno']} | 姓名:{stu_mesall[stu_sno]['name']} | 年龄:{stu_mesall[stu_sno]['age']} | 性别:{stu_mesall[stu_sno]['sex']} | Python成绩:{stu_mesall[stu_sno]['Python']}")
return
else:
ext_func()
# 修改学生信息
def upd_stu():
stu_sno = input("请输入您要修改学生的学号:")
if stu_sno in stu_mesall.keys():
stu_name = input("请输入您要修改学生的姓名:")
stu_age = int(input("请输入您要修改学生的年龄:"))
stu_sex = input("请输入您要修改学生的性别:")
stu_py = int(input("请输入您要修改学生的Python成绩:"))
stu_mes = {"sno": stu_sno, "name": stu_name, "age": stu_age, "sex": stu_sex, "Python": stu_py}
stu_mesall[stu_sno] = stu_mes
with open("学生信息.txt", mode="w", encoding="utf-8") as stu_mesalls:
stu_mesalls.write(str(stu_mesall))
print("修改成功!")
return
else:
ext_func()
# 选择显示功能
def func_stu():
print("""
*********************************************
欢迎进入选择显示功能界面,请输入以下指令:
a. 统计平均分
b. 统计及格率
0\. 回到主菜单
*********************************************
""")
while True:
info_help = input("请输入您要操作的指令:")
if info_help == 'a':
avg_score()
elif info_help == 'b':
pass_score()
elif info_help == '0':
return
else:
print("输入有误,请重新输入!")
# 统计平均分
def avg_score():
score_list = [py_score['Python'] for py_score in stu_mesall.values()]
print(f"学生的平均分:{functools.reduce(lambda x,y : x + y, score_list) / len(score_list)}")
# 统计及格率
def pass_score():
score_list = [py_score['Python'] for py_score in stu_mesall.values()]
print(f"学生的及格率为:{len([score for score in score_list if score >= 60]) / len(score_list) * 100}%")
# 删除学生信息
def del_stu():
stu_sno = input("请输入您要删除学生的学号:")
if stu_sno in stu_mesall.keys():
del stu_mesall[stu_sno]
with open("学生信息.txt", mode="w", encoding="utf-8") as stu_mesalls:
stu_mesalls.write(str(stu_mesall))
print("删除成功!")
return
else:
ext_func()
# 扩展功能
def ext_func():
print("""
*********************************************
您输入的学生学号不存在,请输入以下指令:
a. 重新输入学号
b. 新建学生信息
0\. 回到主菜单
*********************************************
""")
while True:
info_help = input("请输入您要操作的指令:")
if info_help == 'a':
del_stu()
elif info_help == 'b':
add_stu()
elif info_help == '0':
return
else:
print("输入有误,请重新输入!")
# 主函数
def main():
while True:
show_mes()
info_inst = input("请输入您要操作的指令:")
if info_inst == "1":
show_all()
elif info_inst == "2":
add_stu()
elif info_inst == "3":
sel_stu()
elif info_inst == "4":
upd_stu()
elif info_inst == "5":
del_stu()
elif info_inst == "6":
func_stu()
elif info_inst == "7":
return
else:
print("输入指令错误,请重新输入!")
main()
三、文本文件
{‘s001’: {‘sno’: ‘s001’, ‘name’: ‘张三’, ‘age’: 23, ‘sex’: ‘男’, ‘Python’: 45}, ‘s002’: {‘sno’: ‘s002’, ‘name’: ‘李思’, ‘age’: 18, ‘sex’: ‘女’, ‘Python’: 87}}
网友评论