选用list、tuple、dictionary或map等数据结构,存储X个学生的三门课的成绩(机器学习、Python程序设计、研究生英语),并实现以下功能:
stu_number = {
"182085211002":"张三",
"182085211003":"a",
"182085211004":"b",
"182085211005":"a",
"182085211006":"d",
"182085211007":"张三",
"182085211008":"f",
}
stu_info = [
["182085211002","张三","男","汉族","100","99","98"],
["182085211003","a","男","汉族","60","99","98"],
["182085211004","b","男","汉族","100","88","98"],
["182085211005","a","男","汉族","100","99","45"],
["182085211006","d","男","汉族","100","75","98"],
["182085211007","张三","男","汉族","100","75","98"],
["182085211008","f","男","汉族","50","60","60"],
]
rank = {}
# 添加学生信息
def add_stu(stu_id):
stu = []
if stu_id in stu_number.keys(): # 判断'stu_num'是否存在于stu_number的键中
print('该学生已存在,请勿重复添加!')
else:
stu_name = input("请输入学生姓名:")
stu_number[stu_id] = stu_name
stu.append(stu_id)
stu.append(stu_name)
stu.append(input("请输入学生性别:"))
stu.append(input("请输入学生民族:"))
stu.append("无")
stu.append("无")
stu.append("无")
stu_info.append(stu)
print_stu(stu_id)
# 修改学生信息
def modif_stu(stu):
if stu in stu_number.keys(): # 判断'stu_num'是否存在于stu_number的键中
print_stu(stu)
print("该学生信息如上,请输入要修改的项目:")
while True:
cmd = input("1、姓名 2、性别 3、民族 (输入q返回上级菜单):")
if cmd == 'q':
break
elif cmd == '1':
index = find_stu(stu)
for i in index:
stu_info[i][1] = input("请输入修改后的姓名:")
elif cmd == '2':
index = find_stu(stu)
for i in index:
stu_info[i][2] = input("请输入修改后的性别:")
elif cmd == '3':
index = find_stu(stu)
for i in index:
stu_info[i][3] = input("请输入修改后的民族:")
print("修改后的信息如下:")
print_stu(stu)
else:
print('该学生不存在,请添加或重新输入!')
# 删除学生
def delete_stu(stu):
if stu in stu_number.keys(): # 判断'stu_num'是否存在于stu_number的键中
print_stu(stu)
cmd = input("该学生信息如上,确定是否删除?(Y/N):")
if cmd == 'Y':
index = find_stu(stu)
for i in index:
del stu_info[i]
print("该学生已删除!")
else:
print('该学生不存在,请添加或重新输入!')
# 添加学生成绩
def add_stu_val(stu_id):
if stu_id in stu_number.keys(): # 判断'stu_num'是否存在于stu_number的键中
print_stu(stu_id)
print("该学生信息如上")
index = find_stu(stu_id)
for i in index:
stu_info[i][4] = input("请输入机器学习成绩:")
stu_info[i][5] = input("请输入Python程序设计成绩:")
stu_info[i][6] = input("请输入研究生英语:")
print("添加成绩结果如下:")
print_stu(stu)
else:
print('该学生不存在,请添加或重新输入!')
# 修改学生成绩
def modif_stu_val(stu):
if stu in stu_number.keys(): # 判断'stu_num'是否存在于stu_number的键中
print_stu(stu)
print("该学生信息如上,请输入要修改的课程:")
while True:
cmd = input("1、机器学习 2、Python程序设计 3、研究生英语 (输入q返回上级菜单):")
if cmd == 'q':
break
elif cmd == '1':
index = find_stu(stu)
for i in index:
stu_info[i][4] = input("请输入修改后的成绩:")
elif cmd == '2':
index = find_stu(stu)
for i in index:
stu_info[i][5] = input("请输入修改后的成绩:")
elif cmd == '3':
index = find_stu(stu)
for i in index:
stu_info[i][6] = input("请输入修改后的成绩:")
print("修改后的信息如下:")
print_stu(stu)
else:
print('该学生不存在,请添加或重新输入!')
# 排名
def ranking():
to_rank = {}
for stu in stu_info:
if stu[4]=='无' or stu[5]=='无' or stu[6]=='无':
to_rank[stu[0]] = 0
rank[stu[0]] = '无'
else:
to_rank[stu[0]] = (int(stu[4])+int(stu[5])+int(stu[6]))/3
ranked = sorted(to_rank.items(),key = lambda x:x[1],reverse = True)
num = 1
re_num = 0 #分一样
for i in range(len(ranked)):
if i!=0:
if ranked[i][1]==ranked[i-1][1]: #考虑分数一样的情况 排名相同
re_num+=1
rank[ranked[i][0]] = num
else:
num = num + 1 + re_num #分数不一样 排名加一 如果有重复排名则加上
rank[ranked[i][0]] = (num + re_num)
re_num = 0
else: #存第一名
rank[ranked[i][0]] = num
# 按姓名或者学号查找学生,返回值为列表 列表里的值为该学生数据位置
# 如果有重名学生,则返回在列表返回多个学生
def find_stu(stu):
stu_index = []
for i in stu_info:
try:
if i.index(stu) != None: #如果能查找到
stu_index.append(stu_info.index(i))
except:pass
return stu_index
# 根据姓名或者学号显示学生信息及三门课的成绩、排名
def print_stu(stu):
ranking() # 排名
stu_index = find_stu(stu)
if len(stu_index) == 0:
print("无此学生,请核对后重新输入!")
for i in stu_index:
the_stu = stu_info[i]
print("\t姓名:",the_stu[1])
print("\t学号:", the_stu[0])
print("\t性别:", the_stu[2])
print("\t民族:", the_stu[3])
print("\t机器学习成绩:", the_stu[4])
print("\tPython程序设计成绩:", the_stu[5])
print("\t研究生英语成绩::", the_stu[6])
print("\t排名::", rank[the_stu[0]])
print("--------------------------")
def statistics():
machine_learning_max = 0
python_max = 0
english_max = 0
machine_learning_min = 100
python_min = 100
english_min = 100
machine_learning = 0
python = 0
english = 0
num = 0
for stu in stu_info:
if stu[4]!='无' and stu[5]!='无' and stu[6]!='无':
num += 1
machine_learning += float(stu[4])
python += float(stu[5])
english += float(stu[6])
if float(stu[4]) > machine_learning_max: machine_learning_max = float(stu[4])
if float(stu[4]) < machine_learning_min: machine_learning_min = float(stu[4])
if float(stu[5]) > python_max: python_max = float(stu[5])
if float(stu[5]) < python_min: python_min = float(stu[5])
if float(stu[6]) > english_max: english_max = float(stu[6])
if float(stu[6]) < english_min: english_min = float(stu[6])
machine_learning = machine_learning/num
python = python/num
english = english/num
print("学生成绩统计如下:")
print("机器学习:")
print("\t平均分:%.2f,最高分:%.2f,最低分:%.2f" % (machine_learning,machine_learning_max,machine_learning_min))
print("Python程序设计:")
print("\t平均分:%.2f,最高分:%.2f,最低分:%.2f" % (python, python_max, python_min))
print("研究生英语:")
print("\t平均分:%.2f,最高分:%.2f,最低分:%.2f" % (english, english_max, english_min))
while True:
print("--------------------------")
print("请输入以下命令进行操作:")
print("\t1 添加学生信息")
print("\t2 修改学生信息")
print("\t3 删除学生")
print("\t4 添加学生的成绩")
print("\t5 修改学生成绩")
print("\t6 按姓名或者学号查找学生,显示学生信息及三门课的成绩,以及排名")
print("\t7 学生成绩统计")
print("\texit 退出系统!")
cmd = input("请输入命令:")
if cmd == 'exit': # 退出系统!
break
elif cmd == '1': # 添加学生信息
print("--------------------------")
while True:
stu_id = input("请输入要添加学号(输入q返回上级菜单):")
if stu_id == 'q':
break
add_stu(stu_id)
elif cmd == '2': # 修改学生信息
print("--------------------------")
while True:
stu = input("请输入要修改的学生学号(输入q返回上级菜单):")
if stu == 'q':
break
modif_stu(stu)
elif cmd == '3': # 删除学生
print("--------------------------")
while True:
stu = input("请输入要删除的学生学号(输入q返回上级菜单):")
if stu == 'q':
break
delete_stu(stu)
elif cmd == '4': # 添加学生的成绩
print("--------------------------")
while True:
stu = input("请输入要添加成绩的学号(输入q返回上级菜单):")
if stu == 'q':
break
add_stu_val(stu)
elif cmd == '5': # 修改学生成绩
print("--------------------------")
while True:
stu = input("请输入要修改成绩的学号(输入q返回上级菜单):")
if stu == 'q':
break
modif_stu_val(stu)
elif cmd == '6': # 按姓名或者学号查找学生,显示学生信息及三门课的成绩,以及排名
ranking() # 排名
print("--------------------------")
while True:
stu = input("请输入要查询的学生姓名或学号(输入q返回上级菜单):")
if stu == 'q':
break
print_stu(stu)
elif cmd == '7': # 学生成绩统计
print("--------------------------")
statistics()