# 增defadd_stu():# 声明全局变量global stu_infostu_id =int(input("学号:"))stu_name =input("姓名:")stu_sex =input("性别:")stu_score =int(input("成绩:"))# 保证不能有重复的学号存在for stu in stu_info:if stu["stu_id"]== stu_id:print("您输入的学生已经存在,添加失败!!!")returnFalsestu ={"stu_id": stu_id,"stu_name": stu_name,"stu_sex": stu_sex,"stu_score": stu_score}stu_info.append(stu)print("添加成功!!!")returnTrue
2.删除学生基本信息:
# 删defdel_stu():# 声明全局变量global stu_infostu_id =int(input("请输入所删除的学生学号:"))for stu in stu_info:if stu["stu_id"]== stu_id:stu_info.remove(stu)returnTrueprint("您输入的学生学号有误,删除失败!!!")returnFalse
3.查询学生基本信息:
# 查defsearch_stu():# 声明全局变量global stu_infostu_id =int(input("请输入要查询的学生学号:"))for stu in stu_info:if stu["stu_id"]== stu_id:print("stu_id:", stu["stu_id"])print("stu_name:", stu["stu_name"])print("stu_sex:", stu["stu_sex"])print("stu_score:", stu["stu_score"])returnTrueprint("您输入的学生学号不存在,查询失败!!!")returnFalse
4.修改学生基本信息:
# 改defmodify_stu():# 声明全局变量global stu_infostu_id =int(input("请输入要修改的学生学号:"))for stu in stu_info:if stu["stu_id"]== stu_id:stu["stu_id"]=int(input("新的学号:"))stu["stu_name"]=input("新的姓名:")stu["stu_sex"]=input("新的性别:")stu["stu_score"]=int(input("新的成绩:"))returnTrueprint("您输入的学生学号不存在,修改失败!!!")returnFalse
示例代码:
# 学生信息管理系统from typing import List, Any# 定义全局变量stu_info:list[Any]=[]defprint_menu():print("-----------------------------------------------------")print("-------------欢迎使用学生信息管理系统---------------------")print("-----------------1.遍历学生信息------------------------")print("-----------------2.添加学生信息------------------------")print("-----------------3.删除学生信息------------------------")print("-----------------4.查询学生信息------------------------")print("-----------------5.修改学生信息------------------------")print("-----------------6.退出系统---------------------------")print("-----------------------------------------------------")# 增defadd_stu():# 声明全局变量global stu_infostu_id =int(input("学号:"))stu_name =input("姓名:")stu_sex =input("性别:")stu_score =int(input("成绩:"))# 保证不能有重复的学号存在for stu in stu_info:if stu["stu_id"]== stu_id:print("您输入的学生已经存在,添加失败!!!")returnFalsestu ={"stu_id": stu_id,"stu_name": stu_name,"stu_sex": stu_sex,"stu_score": stu_score}stu_info.append(stu)print("添加成功!!!")returnTrue# 删defdel_stu():# 声明全局变量global stu_infostu_id =int(input("请输入所删除的学生学号:"))for stu in stu_info:if stu["stu_id"]== stu_id:stu_info.remove(stu)returnTrueprint("您输入的学生学号有误,删除失败!!!")returnFalse# 查defsearch_stu():# 声明全局变量global stu_infostu_id =int(input("请输入要查询的学生学号:"))for stu in stu_info:if stu["stu_id"]== stu_id:print("stu_id:", stu["stu_id"])print("stu_name:", stu["stu_name"])print("stu_sex:", stu["stu_sex"])print("stu_score:", stu["stu_score"])returnTrueprint("您输入的学生学号不存在,查询失败!!!")returnFalse# 改defmodify_stu():# 声明全局变量global stu_infostu_id =int(input("请输入要修改的学生学号:"))for stu in stu_info:if stu["stu_id"]== stu_id:stu["stu_id"]=int(input("新的学号:"))stu["stu_name"]=input("新的姓名:")stu["stu_sex"]=input("新的性别:")stu["stu_score"]=int(input("新的成绩:"))returnTrueprint("您输入的学生学号不存在,修改失败!!!")returnFalsedefmain():whileTrue:print_menu()ch =int(input("请输入您的选择:"))match ch:case1:print(stu_info)case2:add_stu()case3:del_stu()case4:search_stu()case5:modify_stu()if ch ==6:print("欢迎下次使用本学生信息管理系统!!!")breakreturnNonemain()