学生成绩管理系统主要包括录入学生信息、查找学生信息、删除学生信息、修改学生信息、排序、统计学生总人数、显示学生信息和退出系统。

系统界面编写(菜单显示函数):

def menu():print('============学生信息管理系统============')print('---------------功能菜单---------------')print('\t\t\t1.录入学生信息')print('\t\t\t2.查找学生信息')print('\t\t\t3.删除学生信息')print('\t\t\t4.修改学生信息')print('\t\t\t5.排序')print('\t\t\t6.统计学生总人数')print('\t\t\t7.显示所有学生信息')print('\t\t\t0.退出系统')print('-------------------------------------')

main函数:

def main():while(True):menu()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()

录入学生信息函数:

def insert():stu_list=[]while True:id=input('请输入学生ID(如1001):')if not id:breakname=input('请输入学生姓名:')if not name:breaktry:english=int(input('请输入英语成绩:'))java=int(input('请输入java成绩:'))python=int(input('请输入python成绩:'))except:print('输入无效,不是整数,请重新输入:')continue#将录入的学生成绩保存到字典中student={'id':id,'name':name,'English':english,'Java':java,'Python':python}#将学生信息添加到列表中stu_list.append(student)answer=input('是否继续添加(Y/N):')if answer == 'y' or answer == 'Y':continueelse:break#调用save()函数保存在文件中save(stu_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):mod=input('若根据ID查找学生信息,请输入1;若根据姓名查找学生信息,请输入2:')if mod=='1':id=input('请输入学生ID:')elif mod=='2':name=input('请输入学生姓名:')else:print('输入有误。请重新输入!')search()with open(filename,'r',encoding='utf-8')as rfile:student=rfile.readlines()for item in student:d=dict(eval(item))if id!='':if d['id']==id:student_query.append(d)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{:^10}'print(format_title.format('ID','Name','English','Java','Python','Grade'))# 定义内容的显示格式format_data='{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^10}'for item in lst:print(format_data.format(item.get('id'), item.get('name'), item.get('English'), item.get('Java'), item.get('Python'), int(item.get('English'))+int(item.get('Java'))+int(item.get('Python')) ))

删除学生信息函数:

def delete():while(True):student_id=input("请输入要删除学生的ID:")if student_id!='':if os.path.exists(filename): #判断文件是否存在with open(filename,'r',encoding='utf-8') as file: #存在的话打开student_old=file.readlines()else:student_old=[] #不存在flag=False#标记是否删除if 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:wfile.write(str(d)+'\n')else:flag=Trueif flag:print(f'id为{student_id}的学生信息已被删除!')else:print(f'未查询到id为{student_id}的学生信息。')else:print('无学生信息')breakshow()answer=input('是否继续删除?(Y/N):')if answer=='Y'or answer=='y':continueelse:break

修改学生信息函数:

def 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:')with open(filename,'w',encoding='utf-8') as wfile:for item in student_old:d=dict(eval(item))if d['id']==student_id:print('找到该学生,可以修改相关信息!')while True:try:d['name']=input('请输入学生姓名:')d['English']=input('请输入英语成绩:')d['Java']=input('请输入java成绩:')d['Python']=input('请输入python成绩:')except:print('输入有误,请重新输入')else:breakwfile.write(str(d)+'\n')print('修改成功!')else:wfile.write(str(d) + '\n')answer=input('是否继续修改?(Y/N):')if answer=='y' or answer =='Y':modify()

排序函数:

def sort():show()student_list=[]asc_or_desc=input('请选择排序方式(0为升序,1为降序):')mode=input('请选择排序依据(1为按英语成绩排序,2为按Java成绩排序,3为按Python成绩排序,0为按总成绩排序):')if os.path.exists(filename):with open(filename,'r',encoding='utf-8')as rfile:students=rfile.readlines()for item in students:d=dict(eval(item))student_list.append(d)else:print('暂未保存学生信息!')returnif asc_or_desc=='0':asc_or_desc_bool=Falseelif asc_or_desc=='1':asc_or_desc_bool=Trueelse:print('输入有误,请重新输入:')sort()if mode=='1':student_list.sort(key=lambda x:int(x['English']),reverse=asc_or_desc_bool)#使用匿名函数lambdaelif mode=='2':student_list.sort(key=lambda x:int(x['Java']), reverse=asc_or_desc_bool)elif mode=='3':student_list.sort(key=lambda x:int(x['Python']), reverse=asc_or_desc_bool)elif mode=='0':student_list.sort(key=lambda x:int(x['English'])+int(x['Java'])+int(x['Python']), reverse=asc_or_desc_bool)else:print('输入有误,请重新输入!')sort()show_student(student_list)

统计学生总人数函数:

def total():if os.path.exists(filename):with open(filename,'r',encoding='utf-8')as rfile:students=rfile.readlines()if students==[]:print('暂未录入学生信息,请先录入!')returnelse:print(f'共有{len(students)}名学生!')else:print('暂未保存学生信息!!!')return

显示学生信息函数:

def show():student_list=[]if os.path.exists(filename):with open(filename,'r',encoding='utf-8')as rfile:student_l=rfile.readlines()for item in student_l:student_list.append(eval(item)) #eval还原为字典类型if student_list:show_student(student_list)else:print('暂未保存学生信息!')return

执行:

if __name__ == '__main__':# 只有执行该程序时,才会执行import 是不可以执行的main()

完整源代码:

filename='student.txt'import osdef main():while(True):menu()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()def menu():print('============学生信息管理系统============')print('---------------功能菜单---------------')print('\t\t\t1.录入学生信息')print('\t\t\t2.查找学生信息')print('\t\t\t3.删除学生信息')print('\t\t\t4.修改学生信息')print('\t\t\t5.排序')print('\t\t\t6.统计学生总人数')print('\t\t\t7.显示所有学生信息')print('\t\t\t0.退出系统')print('-------------------------------------')def insert():stu_list=[]while True:id=input('请输入学生ID(如1001):')if not id:breakname=input('请输入学生姓名:')if not name:breaktry:english=int(input('请输入英语成绩:'))java=int(input('请输入java成绩:'))python=int(input('请输入python成绩:'))except:print('输入无效,不是整数,请重新输入:')continue#将录入的学生成绩保存到字典中student={'id':id,'name':name,'English':english,'Java':java,'Python':python}#将学生信息添加到列表中stu_list.append(student)answer=input('是否继续添加(Y/N):')if answer == 'y' or answer == 'Y':continueelse:break#调用save()函数保存在文件中save(stu_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):mod=input('若根据ID查找学生信息,请输入1;若根据姓名查找学生信息,请输入2:')if mod=='1':id=input('请输入学生ID:')elif mod=='2':name=input('请输入学生姓名:')else:print('输入有误。请重新输入!')search()with open(filename,'r',encoding='utf-8')as rfile:student=rfile.readlines()for item in student:d=dict(eval(item))if id!='':if d['id']==id:student_query.append(d)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{:^10}'print(format_title.format('ID','Name','English','Java','Python','Grade'))# 定义内容的显示格式format_data='{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^10}'for item in lst:print(format_data.format(item.get('id'), item.get('name'), item.get('English'), item.get('Java'), item.get('Python'), int(item.get('English'))+int(item.get('Java'))+int(item.get('Python')) ))def delete():while(True):student_id=input("请输入要删除学生的ID:")if student_id!='':if os.path.exists(filename): #判断文件是否存在with open(filename,'r',encoding='utf-8') as file: #存在的话打开student_old=file.readlines()else:student_old=[] #不存在flag=False#标记是否删除if 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:wfile.write(str(d)+'\n')else:flag=Trueif flag:print(f'id为{student_id}的学生信息已被删除!')else:print(f'未查询到id为{student_id}的学生信息。')else:print('无学生信息')breakshow()answer=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:')with open(filename,'w',encoding='utf-8') as wfile:for item in student_old:d=dict(eval(item))if d['id']==student_id:print('找到该学生,可以修改相关信息!')while True:try:d['name']=input('请输入学生姓名:')d['English']=input('请输入英语成绩:')d['Java']=input('请输入java成绩:')d['Python']=input('请输入python成绩:')except:print('输入有误,请重新输入')else:breakwfile.write(str(d)+'\n')print('修改成功!')else:wfile.write(str(d) + '\n')answer=input('是否继续修改?(Y/N):')if answer=='y' or answer =='Y':modify()def sort():show()student_list=[]asc_or_desc=input('请选择排序方式(0为升序,1为降序):')mode=input('请选择排序依据(1为按英语成绩排序,2为按Java成绩排序,3为按Python成绩排序,0为按总成绩排序):')if os.path.exists(filename):with open(filename,'r',encoding='utf-8')as rfile:students=rfile.readlines()for item in students:d=dict(eval(item))student_list.append(d)else:print('暂未保存学生信息!')returnif asc_or_desc=='0':asc_or_desc_bool=Falseelif asc_or_desc=='1':asc_or_desc_bool=Trueelse:print('输入有误,请重新输入:')sort()if mode=='1':student_list.sort(key=lambda x:int(x['English']),reverse=asc_or_desc_bool)#使用匿名函数lambdaelif mode=='2':student_list.sort(key=lambda x:int(x['Java']), reverse=asc_or_desc_bool)elif mode=='3':student_list.sort(key=lambda x:int(x['Python']), reverse=asc_or_desc_bool)elif mode=='0':student_list.sort(key=lambda x:int(x['English'])+int(x['Java'])+int(x['Python']), reverse=asc_or_desc_bool)else:print('输入有误,请重新输入!')sort()show_student(student_list)def total():if os.path.exists(filename):with open(filename,'r',encoding='utf-8')as rfile:students=rfile.readlines()if students==[]:print('暂未录入学生信息,请先录入!')returnelse:print(f'共有{len(students)}名学生!')else:print('暂未保存学生信息!!!')returndef show():student_list=[]if os.path.exists(filename):with open(filename,'r',encoding='utf-8')as rfile:student_l=rfile.readlines()for item in student_l:student_list.append(eval(item)) #eval还原为字典类型if student_list:show_student(student_list)else:print('暂未保存学生信息!')returnif __name__ == '__main__':# 只有执行该程序时,才会执行import 是不可以执行的main()

运行结果:

录入信息截图:

查询信息截图:

统计学生总人数截图:

排序截图: