#coding=utf-8import os.pathfilename='student.txt'def menm():#菜单界面print('===========================学生管理系统===========================')print('-----------------------------功能菜单-----------------------------')print('\t\t\t\t\t\t1.录入学生信息')print('\t\t\t\t\t\t2.查找学生信息')print('\t\t\t\t\t\t3.删除学生信息')print('\t\t\t\t\t\t4.修改学生信息')print('\t\t\t\t\t\t5.排序学生信息')print('\t\t\t\t\t\t6.统计学生人数')print('\t\t\t\t\t\t7.显示学生信息')print('\t\t\t\t\t\t0.退出')print('----------------------------------------------------------------')def insert():#录入学生信息student_list=[]#创建一个新列表while True:id=input('请输入ID(如1001):')if not id:breakname=input('请输入姓名:')if not name:breaktry:english=int(input('请输入英语成绩:'))python=int(input('请输入Python成绩:'))java=int(input('请输入Java成绩:'))except:print('输入无效,不是整数类型,请重新输入')continue#将录入的学生信息保存到字典里student={'id':id,'name':name,'english':english,'python':python,'java':java}#将学生信息添加到列表中student_list.append(student)answer=input('是否继续添加?y/n')if answer=='y' or answer=='Y':continueelse:breaksave(student_list)#将学生信息保存到磁盘文件print('学生信息录入完毕')def save(lst):#保存学生信息try:stu_txt=open(filename,'a',encoding='utf-8')#打开文件,不存在则创建文件,存在则在末尾追加内容except:stu_txt=open(filename,'w',encoding='utf-8')#只写模式打开文件,文件存在覆盖原有内容for item in lst:stu_txt.write(str(item)+'\n')#将内容写进文件stu_txt.close()#关闭文件def search():student_query=[] #创建一个列表while True:id=''name=''if os.path.exists(filename):#文件存在mode=input('按ID查找请输入1,按姓名查找输入2:')if mode=='1':id=input('输入学生ID:')elif mode=='2':name=input('输入学生姓名:')else:print('您的输入有误,请重新输入')search()with open(filename,'r',encoding='utf-8') as rfile:#打开文件,只读模式student=rfile.readlines()#将内容逐行读入for item in student:#将每个内容转换成字典存入dd=dict(eval(item))if id!='':if d['id']==id:student_query.append(d)#以id查找,相同id的存入列表elif name!='':if d['name']==name:student_query.append(d)#以姓名查找,相同名字存入列表#显示查询结果show_student(student_query)#清空列表student_query.clear()answer=input('是否继续查询?y/n\n')if answer=='y' or answer=='Y':continueelse:breakelse:print('暂未保存学生信息')returndef show_student(lst):#展示学生信息if len(lst)==0:#列表为空,说明没有学生信息print('没有查询到学生信息,无法显示!!!')return#定义标题显示格式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}'for item in lst:#将列表中的内容逐行输出print(format_data.format(item.get('id'), item.get('name'), item.get('english'), item.get('python'), item.get('java'), int(item.get('english'))+int(item.get('python'))+int(item.get('java')) ))def delete():#删除学生信息while True:student_id=input('请输入要删除的学生ID:')if student_id!='':#ID不为空if os.path.exists(filename):#文件存在with open(filename,'r',encoding='utf-8') as file:student_old=file.readlines()#将内容逐行读入else:student_old=[]#文件不存在,则为空列表flag=Falseif student_old:#列表不为空with open(filename,'w',encoding='utf-8') as wfile:#以只写模式 打开文件夹 覆盖原先内容d={}#创建一个空字典for item in student_old:#将列表中的内容逐行读入d=dict(eval(item)) #将字符串转为字典if d['id']!=student_id:#若该id不为所查id,则重新写入wfile.write(str(d)+'\n')else:#若id为所查id则不写入,且flag改为Trueflag=Trueif flag:print(f'ID为{student_id}的学生信息已被删除')else:print(f'没有找到ID为{student_id}的学生信息')else:print('无学生信息')breakanswer=input('是否继续删除y/n')if answer=='y' or answer=='Y':continueelse:breakdef modify():#修改学生信息show()#先展示所有学生信息if os.path.exists(filename):#文件存在with open(filename,'r',encoding='utf-8',) as rfile:#以只读方式打开文件夹student_old=rfile.readlines()#逐行读入文件else:returnstudent_id=input('请输入要修改的学生ID:')#需要修改的学生IDwith open(filename,'w',encoding='utf-8') as wfile:#以只写方式 打开文件夹 覆盖内容for item in student_old:#将列表内容逐个读入d=dict(eval(item))#转换成字典ifd['id']==student_id:#该id为所要修改的学生idprint('找到学生信息,可以修改其相关信息!')try:d['name']=input('请输入姓名:')d['english']=input('请输入英语成绩:')d['python']=input('请输入Python成绩:')d['java']=input('请输入java成绩:')except:print('您输入有误,请重新输入!!!')wfile.write(str(d)+'\n')#将其写入文件print('修改成功!!!')else:wfile.write(str(d)+'\n')#id不为所要修改的学生idanswer=input('是否继续修改其它学生信息?y/n\n')if answer=='y' or answer=='Y':modify()def sort():#按成绩排序学生信息show()#展示所有学生信息if os.path.exists(filename):#文件存在with open(filename,'r',encoding='utf-8') as rfile:#以只读模式 打开文件student_list=rfile.readlines()#将内容逐行保存student_new=[]#创建一个新列表for item in student_list:#将列表内容逐个读入d=dict(eval(item))#转为字典student_new.append(d)#将其后接保存到新列表else:returnasc_or_desc=input('请选择(0.升序 1.降序):')if asc_or_desc=='0':asc_or_desc_bool=False#升序bool值为Falseelif asc_or_desc=='1':asc_or_desc_bool=True#降序bool值为Trueelse:print('您的输入有误,请重新输入')sort()mode=input('请选择排序方式(1.按英语成绩排序 2.按Python成绩排序 3.按Java成绩排序 4.按总成绩排序)')if mode=='1':student_new.sort(key=lambda x:int(x['english']), reverse=asc_or_desc_bool)elif mode=='2':student_new.sort(key=lambda x: int(x['python']), reverse=asc_or_desc_bool)elif mode=='3':student_new.sort(key=lambda x: int(x['java']), reverse=asc_or_desc_bool)elif mode=='4':student_new.sort(key=lambda x: int(x['english'])+int(x['python'])+int(x['java']), reverse=asc_or_desc_bool)else:print('输入有误,请重新输入')sort()show_student(student_new)#展示排序后的学生信息def total():#统计学生个数if os.path.exists(filename):#文件存在with open(filename,'r',encoding='utf-8') as rfile:#以只读方式 打开文件夹students=rfile.readlines()#逐行读入if students:print('一共有{}名学生'.format(len(students)))#用len内置函数else:print('还没有录入学生信息')else:print('暂未保存数据信息...')def show():student_list=[]#创建空列表if os.path.exists(filename):#文件存在with open(filename,'r',encoding='utf-8') as rfile:#以只读模式 打开文件students=rfile.readlines()#逐行读入for item in students:#将列表内容逐个读入student_list.append(eval(item))#后接入列表if student_list:#列表不为空show_student(student_list)#展示信息def main():#主函数while True:menm()choice=int(input('请选择'))if choice in [0,1,2,3,4,5,6,7]:if choice==0:answer=input('您确认要退出系统吗?y/n')if answer=='y' or answer== 'Y':print('谢谢您的使用!!!')breakelse:continueelif choice==1:insert()elif choice==2:search()elif choice==3:delete()elif choice==4:modify()elif choice==5:sort()elif choice==6:total()elif choice==7:show()if __name__ == '__main__':main()