您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > Python学生管理系统-文件保存版
#coding=utf-8students=[]defstu_info_print():print('*'*50)print('''1.添加某学生信息2.修改某学生信息3.查询某学生成绩4.查询某学生信息5.查询所有学生信息7.退出系统''')print('*'*50)#定义保存学生信息到文件defsave_stu(students):f=open('students_info.txt','w')f.write(str(students))f.close()#加载学生信息defload_stu():globalstudentstry:f=open('students_info.txt')students=eval(f.read())f.close()exceptException:pass#添加学生信息defadd_student():student={}whileTrue:student['name']=input(请输入添加的学成姓名:)ifnotstudent['name']:breakwhileTrue:student['sex']=input(请输入学生性别(M/F):)ifstudent['sex']=='M'orstudent['sex']=='F':breakelse:print(输入有误,请重新输入)continuewhileTrue:try:student['age']=int(input(请输入学生年龄:))student['stu_id']=int(input(请输入学生学号:))student['score']=int(input(请输入学生成绩:))exceptExceptionase:ifnote:breakelse:print(输入无效,不是整形数值....重新录入信息)continuebreakbreakstudents.append(student)save_flag=input(是否要保存新添加的学生信息(Y/N):)ifsave_flag=='Y':save_stu(students)else:print(未能保存新加学生信息!!!)#修改学生信息defupdate_stu():update_flag=0name=input(请输入被修改学生的姓名:)forstudentinstudents:ifstudent['name']==name:update_flag=1whileTrue:try:num=int(input(请输入需要修改参数的序号1:姓名;2:性别;3:年龄;4:学号;5:成绩;6:退出:))exceptExceptionase:ifnote:breakelse:print(输入无效,不是整形数值....重新录入信息)continueifnum==1:student['name']=input(请输入添加的学成姓名:)ifnotstudent['name']:breakelifnum==2:whileTrue:student['sex']=input(请输入学生性别(M/F):)ifstudent['sex']=='M'orstudent['sex']=='F':breakelse:print(输入有误,请重新输入)continueelifnum==3:whileTrue:try:student['age']=int(input(请输入学生年龄:))exceptExceptionase:ifnote:breakelse:print(输入无效,不是整形数值....重新录入信息)continuebreakelifnum==4:whileTrue:try:student['stu_id']=int(input(请输入学生学号:))exceptExceptionase:ifnote:breakelse:print(输入无效,不是整形数值....重新录入信息)continuebreakelifnum==5:whileTrue:try:student['score']=int(input(请输入学生成绩:))exceptExceptionase:ifnote:breakelse:print(输入无效,不是整形数值....重新录入信息)continuebreakelse:breaksave_flag=input(是否要保存新添加的学生信息(Y/N):)ifsave_flag=='Y':save_stu(students)else:print(未能保存新加学生信息!!!)ifupdate_flag==0:print(学生不存在,无法修改!!!)#查询学生分数defselect_stu_score():select_flag=0name=input(请输入被修改学生的姓名:)forstudentinstudents:ifstudent['name']==name:select_flag=1print('该同学的分数是:',student['score'])ifselect_flag==0:print(查询的学生不存在!!!)#查询学生信息defselect_stu_info():select_flag=0name=input(请输入被修改学生的姓名:)forstuinstudents:ifstu['name']==name:select_flag=1print('姓名\t性别\t年龄\t学号\t\t成绩')print('%s\t%s\t\t%d\t%d\t%d'%(stu['name'],stu['sex'],stu['age'],stu['stu_id'],stu['score']))ifselect_flag==0:print(查询的学生不存在!!!)#打印所有学生的信息defprint_students():print('姓名\t性别\t年龄\t学号\t\t成绩')forstuinstudents:print('%s\t%s\t\t%d\t%d\t%d'%(stu['name'],stu['sex'],stu['age'],stu['stu_id'],stu['score']))defmain():stu_info_print()load_stu()whileTrue:try:num=int(input(请输入菜单编号:))exceptExceptionase:ifnote:breakelse:print(输入无效,不是整形数值....重新录入信息)continueifnum==1:add_student()elifnum==2:update_stu()elifnum==3:select_stu_score()elifnum==4:select_stu_info()elifnum==5:print_students()else:breakif__name__=='__main__':main()
本文标题:Python学生管理系统-文件保存版
链接地址:https://www.777doc.com/doc-7340989 .html