袁萌 2019-03-19
本文实例为大家分享了python学生管理系统的具体代码,供大家参考,具体内容如下
基于列表存储的学生管理系统,实现如下功能
==================
学生管理系统
1、添加学生信息
2、删除学生信息
3、查询学生信息
4、修改学生信息
5、显示所有学生信息
6、退出
==================
代码如下:
import re student=[] def appendStudentInfo(): studentinfo={"Name":"","ID":"","语文":"","数学":"","英语":"","总分":""} studentinfo["Name"]=input("请输入学生姓名:") idflag=True while idflag: studentinfo["ID"] = input("请输入学生学号:") pattern=re.compile("^\d{3}$") if not re.match(pattern,studentinfo["ID"]) : print("输入错误,请重新输入") idflag = True if querystudent(studentinfo["ID"])==True: print("该学号已经存在请重新输入") idflag = True if querystudent(studentinfo["ID"])==False and re.match(pattern,studentinfo["ID"]): idflag = False studentinfo["语文"]=input("请输入语文成绩:") while not studentinfo["语文"].isdigit() or int(studentinfo["语文"])>100 or int(studentinfo["语文"])<0: studentinfo["语文"] = input("输入错误,请重新输入:") studentinfo["数学"]=input("请输入数学成绩:") while not studentinfo["数学"].isdigit() or int(studentinfo["数学"]) > 100 or int(studentinfo["数学"]) < 0: studentinfo["数学"] = input("输入错误,请重新输入:") studentinfo["英语"]=input("请输入英语成绩:") while not studentinfo["英语"].isdigit() or int(studentinfo["英语"]) > 100 or int(studentinfo["英语"]) < 0: studentinfo["英语"] = input("输入错误,请重新输入:") studentinfo["总分"]=int(studentinfo["语文"])+int(studentinfo["英语"])+int(studentinfo["数学"]) student.append(studentinfo) def delstudent(): delstudentid = input("请输入要删除的学生学号:") flag =False for item in student: if item["ID"]==delstudentid: flag = True print("要删除学生的相关信息如下:") print(item) select=input("是否删除:是(Y)/否(N)") if select=="Y" or select=="y": student.remove(item) print("删除成功") elif select =="N" or select=="n": print("取消删除") else: print("输入错误") if flag ==False: print("未搜索到该学生") def querystudent(querystudentid): flag=False for item in student: if item["ID"]==querystudentid: flag=True return flag def modifystudentifo(): delstudentid = input("请输入要修改的学生学号:") flag=False for item in student: if item["ID"]==delstudentid: print("查询内容如下:") print(item) flag=True while True: modifymenu = input("请输入修改选项:1、姓名,2、语文成绩,3、数学成绩,4、英语成绩,5、退出") while not modifymenu.isdigit(): modifymenu = input("输入错误,请重新输入:") if int(modifymenu)==1: item["Name"] = input("请重新输入学生姓名:") elif int(modifymenu)==2: item["语文"] = input("请重新输入学生语文成绩:") elif int(modifymenu)==3: item["数学"] = input("请重新输入学生数学成绩:") elif int(modifymenu)==4: item["英语"] = input("请重新输入学生英语成绩:") elif int(modifymenu) == 5: break else: print("输入序号无效") item["总分"] = int(item["语文"]) + int(item["英语"]) + int(item["数学"]) print("修改结果如下:") print(item) if flag ==False: print("未搜索到该学生") def allinfo(): for item in student: print(item) def iteminfo(querystudentid): for item in student: if item["ID"]==querystudentid: print("查询内容如下:") print(item) def studentMenu(): print("="*30) print("学生管理系统") print("1、添加学生信息") print("2、删除学生信息") print("3、查询学生信息") print("4、修改学生信息") print("5、显示所有学生信息") print("6、退出") print("="*30) if __name__ == '__main__': while True: studentMenu() menuindex = input("请输入选项序号:") while not menuindex.isdigit(): menuindex = input("输入错误,请重新输入:") if int(menuindex) ==1: appendStudentInfo() elif int(menuindex) ==2: delstudent() elif int(menuindex) ==3: querystudentid = input("请输入要查询的学生学号:") if querystudent(querystudentid) == True: iteminfo(querystudentid) else:print("未搜索到该学生") elif int(menuindex) ==4: modifystudentifo() elif int(menuindex)== 5: allinfo() elif int(menuindex) == 6: break else: print("输入序号无效")