# student_info.py# 学生信息管理项目,要求带操作界面:# +----------------------+# | 1)添加学生信息 |# | 2)显示所有学生的信息 |# | 3)删除学生信息 |# | 4)修改学生信息 |# | 5)按学生成绩高-低显示学生信息 |# | 6)按学生成绩低-高显示学生信息 |# | 7)按学生年龄高-低显示学生信息 |# | 8)按学生年龄低-高显示学生信息 |# | 9)退出 | def meun(): menu_info = '''+----------------------+| 1)添加学生信息 || 2)显示所有学生的信息 || 3)删除学生信息 || 4)修改学生信息 || 5)按学生成绩高-低显示学生信息 || 6)按学生成绩低-高显示学生信息 || 7)按学生年龄高-低显示学生信息 || 8)按学生年龄低-高显示学生信息 || 9)退出 |''' print(menu_info) # 以下二个函数用于sorted排序, key的表达式函数def get_age(*l): for x in l: return x.get("age")def get_score(*l): for x in l: return x.get("score") # 1)添加学生信息def add_student_info(): L = [] while True: n = input("请输入名字:") if not n: # 名字为空 跳出循环 break a = int(input("请输入年龄:")) s = int(input("请输入成绩:")) info = {"name":n,"age":a,"score":s} L.append(info) return L # 2)显示所有学生的信息def show_student_info(student_info): print("名字".center(8),"年龄".center(4),"成绩".center(4)) for info in student_info: print(info.get("name").center(10),str(info.get("age")).center(4),str(info.get("score")).center(4)) # 3)删除学生信息def del_student_info(student_info,del_name = ''): if not del_name: del_name = input("请输入删除的学生姓名:") for info in student_info: if del_name == info.get("name"): return info # 4)修改学生信息def mod_student_info(student_info): mod_name = input("请输入修改的学生姓名:") for info in student_info: if mod_name == info.get("name"): a = int(input("请输入年龄:")) s = int(input("请输入成绩:")) info = {"name":mod_name,"age":a,"score":s} return info raise IndexError("学生信息不匹配,没有找到%s" %mod_name) # 5)按学生成绩高-低显示学生信息def score_reduce(student_info): print("按学生成绩高-低显示") mit = sorted(student_info ,key = get_score,reverse = True) show_student_info(mit) # 6)按学生成绩低-高显示学生信息def score_rise(student_info): print("按学生成绩低-高显示") mit = sorted(student_info ,key = get_score) show_student_info(mit) # 7)按学生年龄高-低显示学生信息def age_reduce(student_info): print("按学生年龄高-低显示:") mit = sorted(student_info ,key = get_age,reverse = True) show_student_info(mit) # 8)按学生年龄低-高显示学生信息def age_rise(student_info): print("按学生年龄低-高显示:") mit = sorted(student_info ,key = get_age) show_student_info(mit) def main(): student_info = [] while True: meun() number = input("请输入选项:") if number == '1': student_info = add_student_info() elif number == '2': show_student_info(student_info) elif number == '3': student_info.remove(del_student_info(student_info)) elif number == '4': try: student = mod_student_info(student_info) except Exception as e: # 学生姓名不匹配 print(e) else: # 首先按照根据输入信息的名字,从列表中删除该生信息,然后重新添加该学生最新信息 student_info.remove(del_student_info(student_info,del_name = student.get("name"))) student_info.append(student) elif number == '5': score_reduce(student_info) elif number == '6': score_rise(student_info) elif number == '7': age_reduce(student_info) elif number == '8': age_rise(student_info) elif number == '9': break else: break main()