import time # def print_info(): print("=" * 30) print("欢迎使用学生管理系统") print("1. 添加学生信息") print("2. 修改学生信息") print("3. 删除学生信息") print("4. 查询学生信息") print("5. 计算平均年龄") print("6. 退出系统") print("=" * 30) def add_student(): # 添加学生信息 global student_info # 声明函数中要修改的是全局变量student_info dict_1 = {} name = input("请输入学生的姓名:") age = int(input("请输入学生的年龄:")) sex = input("请输入学生的性别:") id = input("请输入学生的号码:") dict_1["name"] = name # 通过key添加value dict_1["age"] = age dict_1["sex"] = sex dict_1["id"] = id student_infor.append(dict_1) # 将字典追加到列表的一个元素 # student_infor = [{'name': 'zhangsna ', 'age': '20', 'sex': 'nv', 'id': '123456y'}] def alter_student(): list_2= [] opp_name = input("请输入要删除的学生姓名") for i in student_infor: for k, v in i.items(): if k == 'name': list_2.append({k: v}) num_3 = list_2.index({"name": opp_name}) # global student_infor name = input("请输入新的学生的姓名:") if len(name) > 0: student_infor[num_3]["name"] = name age = input("请输入新的年龄") if len(age) > 0: student_infor[num_3]["age"] = age id = input("请输入新的id") if len(id) > 0: student_infor[num_3]["id"] = id sex = input("请输入新的性别") if len(sex) > 0: student_infor[num_3]["sex"] = sex def opp_student(): list_2 = [] opp_name = input("请输入要删除的学生姓名") for i in student_infor: for k, v in i.items(): if k == 'name': list_2.append({k: v}) num_3 = list_2.index({"name": opp_name}) # global student_infor del student_infor[num_3] def show_student(): # 显示学生信息 num_1= input("根据序号查询还是根据姓名查询(0根据序号,1根据姓名,2显示所有)") if num_1 == "1": list_2 = [] new_name = input("请输入要查查询的学生姓名") for i in student_infor: for k, v in i.items(): if k == 'name': list_2.append({k: v}) num_3 = list_2.index({"name": new_name}) print(student_infor[num_3]) elif num_1 == "0": show_num = int(input("请输入您要查询的学生序号:(0代表查询所有的学生信息)")) if show_num != 0: for i, j in student_infor[show_num - 1].items(): print(i, j) elif show_num == 0: for temp in student_infor: # 遍历列表 m = 1 # 索引 for x, y in temp.items(): print(m, x, y) m += 1 time.sleep(2) elif num_1 == "2": print(student_infor) else: print("输入错误") def age_1(): m = 0 n = 0 for i in student_infor: for v in i.values(): if type(v) == int: # print(v) m += v n += 1 print(m / n) student_infor = [] while True: print_info() choise = int(input('请输入您要选择的操作:(1~5)')) if choise in [1, 2, 3, 4, 5, 6]: print("您选择的操作是", choise) if choise == 6: print("感谢使用该程序") num_1 = input("您确定要退出程序吗:(y/n)") if num_1 == "y": break elif num_1 == "n": continue elif choise == 1: print("您选择的操作是1") add_student() print(student_infor) elif choise == 2: print("您选择的操作是修改学生信息") alter_student() print(student_infor) elif choise == 3: print("您选择的操作是删除学生信息") opp_student() elif choise == 4: print("您选择的操做是查看学生信息") show_student() elif choise == 5: print("您选择的是计算平均年龄") age_1() else: print("输入有误请重新输入"