如何用python实现简单的学生成绩管理系统
导读:本文共4247字符,通常情况下阅读需要14分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: 需求:代码:importosfilename='student.txt'defmain():whileTrue:menu()choice=int(input("请选择:"))ifchoicein[0,1,2,3,4,5,6,7]:ifchoice==0:answer=input("你确定要退出吗?y... ...
音频解说
目录
(为您整理了一些要点),点击可以直达。需求:
代码:
importosfilename='student.txt'defmain():whileTrue:menu()choice=int(input("请选择:"))ifchoicein[0,1,2,3,4,5,6,7]:ifchoice==0:answer=input("你确定要退出吗?y/n")ifanswer=='y'oranswer=='Y':print("谢谢使用!")breakelse:continueelifchoice==1:insert()elifchoice==2:search()elifchoice==3:delete()elifchoice==4:modify()elifchoice==5:sort()elifchoice==6:total()else:show()#菜单defmenu():print("====================学生成绩管理系统=========================")print("======================功能菜单==============================")print("\t\t1、录入学生信息")print("\t\t2、查找学生信息")print("\t\t3、删除学生信息")print("\t\t4、修改学生信息")print("\t\t5、排序")print("\t\t6、统计学生人数")print("\t\t7、显示所有学生信息")print("\t\t0、退出系统")print("--------------------------------------------------------------")#插入definsert():student_list=[]whileTrue:id=input('请输入ID(如1001):')ifnotid:breakname=input('请输入姓名:')ifnotname:breaktry:english=int(input("inputenglishgrade:"))python=int(input("inputpythongrade:"))java=int(input("inputjavagrade:"))except:print("输入成绩无效!重新输入!")continuestudent={'id':id,'name':name,'english':english,'python':python,'java':java}student_list.append(student)answer=input("是否继续添加信息!?:'y/n'")ifanswer=='y':continueelse:break#调用save函数将其保存在文本当中save(student_list)print("学生信息录入成功!")#保存defsave(lst):try:stu_txt=open(filename,'a',encoding='utf-8')except:stu_txt=open(filename,'w',encoding='utf-8')foriteminlst:stu_txt.write(str(item)+'\n')stu_txt.close()#搜索defsearch():whileTrue:student_name=input("请输入你要查找的学生姓名:")ifstudent_name:ifos.path.exists(filename):withopen(filename,'r',encoding='utf-8')asrfile:student_old=rfile.readlines()else:student_old=[]ifstudent_old:d={}flag=Falseforiteminstudent_old:d=dict(eval(item))ifd['name']==student_name:flag=Truestudent_show(d)else:passelse:print("无学生信息,请添加!")ifflag:print("查询成功")else:print("查询失败")else:print("没有输入学生姓名!")answer=input("请问是否继续查询:y/n")ifanswer=="y":continueelse:break#展示个人信息defstudent_show(dic):format_title='{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'print(format_title.format('ID','姓名','英语成绩','python成绩','java成绩','总成绩'))format_data='{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'print(format_data.format(dic.get('id'),dic.get('name'),dic.get('english'),dic.get('python'),dic.get('java'),int(dic.get('english'))+int(dic.get('python'))+int(dic.get('java')),))#删除defdelete():whileTrue:student_id=input("请输入要删除的学生ID:")ifstudent_id:ifos.path.exists(filename):withopen(filename,'r',encoding='utf-8')asfile:student_old=file.readlines()else:student_old=[]flag=Falseifstudent_old:withopen(filename,'w',encoding='utf-8')aswfile:d={}foriteminstudent_old:d=dict(eval(item))ifd['id']!=student_id:wfile.write(str(d)+"\n")else:flag=Trueifflag:print(f"{student_id}已经被删除!")else:print(f"{student_id}没有找到!")else:print("无学生信息!")breakshow()answer=input("是否继续删除?y/n")ifanswer=='y':continueelse:breakelse:print("请输入要删除的ID:")continue#修改defmodify():whileTrue:student_id=input("请输入要修改的学生ID:")ifstudent_id:ifos.path.exists(filename):withopen(filename,'r',encoding='utf-8')asfile:student_old=file.readlines()else:student_old=[]ifstudent_old:withopen(filename,'w',encoding='utf-8')aswfile:d={}foriteminstudent_old:d=dict(eval(item))ifd['id']==student_id:answer=input('已经找到该学生信息,是否修改:y/n?')flag=Falseifanswer:d['name']=input("姓名:")d['english']=int(input("english:"))d['python']=int(input("python:"))d['java']=int(input("java:"))wfile.write(str(d)+"\n")flag=Trueelse:wfile.write(str(d)+"\n")breakelse:wfile.write(str(d)+"\n")ifflag:print("修改成功!")else:print("修改失败!")else:print("无学生信息,请添加")answer=input("请问是否继续修改?y/n")ifanswer=="y":continueelse:breakelse:print("输入错误!")#排序defsort():whileTrue:ifos.path.exists(filename):withopen(filename,'r',encoding='utf-8')asfile:student_old=file.readlines()student_new=[]d={}foriteminstudent_old:d=dict(eval(item))student_new.append(d)asc_and_desc=int(input("怎么排序:0升序1降序:"))choice=int(input("按照什么排序:1english2python3java0总成绩:"))ifchoice==1:student_new.sort(key=lambdax:int(x['english']),reverse=asc_and_desc)elifchoice==2:student_new.sort(key=lambdax:int(x['python']),reverse=asc_and_desc)elifchoice==3:student_new.sort(key=lambdax:int(x['java']),reverse=asc_and_desc)elifchoice==0:student_new.sort(key=lambdax:int(x['english'])+int(x['python'])+int(x['java']),reverse=asc_and_desc)else:print("输入有误")sort()else:print("无学生信息")format_title='{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'print(format_title.format('ID','姓名','英语成绩','python成绩','java成绩','总成绩'))format_data='{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'fordicinstudent_new:print(format_data.format(dic.get('id'),dic.get('name'),dic.get('english'),dic.get('python'),dic.get('java'),int(dic.get('english'))+int(dic.get('python'))+int(dic.get('java')),))answer=input("请问是否继续排序?y/n")ifanswer=="y":continueelse:break#总人数deftotal():ifos.path.exists(filename):withopen(filename,'r',encoding='utf-8')asfile:student_old=file.readlines()print('一共有',len(student_old),'名学生!')else:print("无人员资料")importmsvcrtmsvcrt.getch()#展示defshow():ifos.path.exists(filename):withopen(filename,'r',encoding='utf-8')asfile:student_old=file.readlines()d={}students=[]foriteminstudent_old:d=dict(eval(item))students.append(d)else:print("没有信息!")iflen(students):format_title='{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'print(format_title.format('ID','姓名','英语成绩','python成绩','java成绩','总成绩'))format_data='{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'fordicinstudents:print(format_data.format(dic.get('id'),dic.get('name'),dic.get('english'),dic.get('python'),dic.get('java'),int(dic.get('english'))+int(dic.get('python'))+int(dic.get('java')),))else:print("无人员信息!")importmsvcrtmsvcrt.getch()if__name__=='__main__':main()
</div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:
如何用python实现简单的学生成绩管理系统的详细内容,希望对您有所帮助,信息来源于网络。