Python学生成绩管理系统

发布时间 2023-12-24 10:52:35作者: du463
filename='student.txt'  
import os  
def menu():  
    print('--------------学生管理系统-----------')  
    print('--------------功能菜单--------------')  
    print('1.录入学生信息')  
    print('2.查找学生信息')  
    print('3.删除学生信息')  
    print('4.修改学生信息')  
    print('5.分数排序')  
    print('6.统计学生人数')  
    print('7.显示全部学生信息')  
    print('0.退出系统')  
    print('-------------------------------------')  
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':  
                    print('退出系统,谢谢使用')  
                    break  
                else:  
                    continue  
            elif 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():      #录入学生信息  
    student_list=[]  
    while True:  
        id=input('请输入学号:')  
        if not id:  
            break  
        name=input('请输入姓名:')  
        if not name:  
            break  
        try:  
            python=int(input('请输入python成绩:'))  
            math=int(input('请输入高数成绩:'))  
            english=int(input('请输入英语成绩:'))  
        except:  
            print('成绩非整数类型,重新输入')  
            continue  
        student={'id':id,'name':name,'python':python,'math':math,'english':english}  
        #将学生信息添加到列表中  
        student_list.append(student)  
        answer=input('是否继续添加?(Y/N):')  
        if answer=='Y':  
            continue  
        else:  
            break  
    save(student_list)  
    print('学生信息录入完毕')  
  
def save(lst):  
    try:  
        student_txt=open(filename,'a')  #如果文件存在,则追加信息  
    except:  
        student_txt=open(filename,'w')  #如果文件不存在就以写入形式打开  
    for item in lst:  
        student_txt.write(str(item)+'\n')  
    student_txt.close()  
def search():         #查找学生信息  
    student_list=[]  
    while True:  
        id=' '  
        name=' '  
        if os.path.exists(filename):  
            mode=input('按照学号查找请输入1,按照姓名查找请输入2')  
            if mode=='1':  
                id=input('请输入学生学号:')  
            elif mode=='2':  
                name=input('请输入学生姓名:')  
            else:  
                print('输入有误,请重新输入')  
                search()  
            with open(filename,'r') as file:  
                student=file.readlines()  
                for item in student:  
                    d=dict(eval(item))  
                    if id!=' ':  
                        if d['id']==id:  
                            student_list.append(d)  
                    elif name!=' ':  
                        if d['name']==name:  
                            student_list.append(d)  
            show_res(student_list)  
            student_list.clear()  
            answer=input('是否要继续查找学生信息?(Y/N)')  
            if answer=='Y':  
                continue  
            else:  
                break  
        else:  
            print('没有保存学生信息')  
            return  
def show_res(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成绩','高数成绩','英语成绩','总成绩'))  
    #定义内容显示格式  
    format_data='{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^14}\t{:^14}'  
    for item in lst:  
        print(format_data.format(item.get('id'),  
                                 item.get('name'),  
                                 item.get('python'),  
                                 item.get('math'),  
                                 item.get('english'),  
                                 int(item.get('python'))+int(item.get('math'))+int(item.get('english'))  
                                 ))  
def delete():     #删除学生信息  
    while True:  
        student_id=input('请输入删除学生的学号:')  
        if student_id!=' ':  
            if os.path.exists(filename):  
                with open(filename,'r') as file:  
                    student_massage=file.readlines()  
            else:  
                student_massage=[]  
            flag=False  
            if student_massage:  
                with open(filename,'w') as file:  
                    d={}  
                    for item in student_massage:  
                        d=dict(eval(item))  
                        if ['id']!=student_id:  
                            file.write(str(d)+'\n')  
                        else:  
                            flag=True    #表示删除  
                    if flag:  
                        print(f'学号{student_id}已经删除')  
                    else:  
  
                        print(f'没有找到学号为{student_id}的信息')  
        else:  
            print('无学生信息')  
            break  
        show()  
        answer=input('是否继续删除?(Y/N)')  
        if answer=='Y':  
            continue  
        else:  
            break  
def modify():    #修改学生信息功能  
    show()  
    if os.path.exists(filename):  
        with open(filename,'r') as file:  
            student_massage=file.readlines()  
    else:  
        return  
    student_id=input('请输入要修改的学生学号:')  
    with open(filename,'w') as wfile:  
        for item in student_massage:  
            d=dict(eval(item))  
            if d['id']==student_id:  
                print('找到该学生信息,可以进行修改')  
                while True:  
                    try:  
                        d['name']=input('请输入姓名')  
                        d['python']=input('输入python成绩')  
                        d['math']=input('输入math成绩')  
                        d['english']=input('输入english成绩')  
                    except:  
                        print('输入有误,请重新输入')  
                    else:  
                        break  
                wfile.write(str(d)+'\n')  
                print('修改成功')  
            else:  
                wfile.write(str(d)+'\n')  
        answer=input('是否继续修改学生信息?(Y/N)')  
        if answer=='Y':  
            modify()  
def sort():  
    show()  
    if os.path.exists(filename):  
        with open(filename,'r') as file:  
            student_lst=file.readlines()  
        student_new=[]  
        for item in student_lst:  
            d=dict(eval(item))  
            student_new.append(d)  
    else:  
        return  
    order=input('请选择0.升序 1.降序:')  
    if order=='0':  
        order_bool=False  #升序  
    elif order=='1':  
        order_bool=True   #降序  
    else:  
        print('输入有误,请重新输入')  
        sort()  
    mode=input('请选择排序方式1.按照python成绩,2.按照高数成绩,3.按照英语成绩,4.按照总成绩:')  
    if mode=='1':  
        student_new.sort(key=lambda x:int(x['python']),reverse=order_bool)   #使用python成绩进行排序  
    elif mode=='2':  
        student_new.sort(key=lambda x: int(x['math']), reverse=order_bool)  
    elif mode=='3':  
        student_new.sort(key=lambda x: int(x['english']), reverse=order_bool)  
    elif mode=='4':  
        student_new.sort(key=lambda x: int(x['python']+int(x['math'])+int(x['english'])), reverse=order_bool)  
    else:  
        print('输入有误,请重新输入')  
        sort()  
    show_res(student_new)  
def total():         #学生总人数  
    if os.path.exists(filename):  
        with open(filename,'r') as file:  
            students=file.readlines()  
            if students:  
                print(f'一共有{len(students)}名学生')  
            else:  
                print('还没有录入学生信息')  
    else:  
        print('暂未保存学生信息')  
def show():  
   student_list=[]  
   if os.path.exists(filename):  
       with open(filename,'r') as file:  
           students=file.readlines()  
           for item in students:  
               student_list.append(eval(item))  
           if student_list:  
                show_res(student_list)  
   else:  
       print('暂未保存数据信息')  
  
  
if __name__ == '__main__':  
    main()