基于python的学生信息管理系统

2021/7/16 22:06:42

本文主要是介绍基于python的学生信息管理系统,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

基于python的学生信息管理系统

源码
"""
    Welcome to Python
    Life is short , You need python   
    author: MX_INFO
    version: 1.0
"""
import os
#文件路径
filename="student.txt"
#主菜单
def mainMenu():
    while True:
        menu()
        choice=int(input("请选择:"))
        if choice in range(8):
            if choice==0:
                answer=input("你确定退出系统吗?y/n")
                if answer=='y' or 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()
        else:
            print("你的输入有误~~~")

#添加学生信息
def insert():
    student_lst=[]
    while True:
        id=input("请输入学生ID:")
        if not id:
            break
        name=input("请输入学生姓名:")
        if not name:
            break
        try:
            english=int(input("请输入英语成绩:"))
            java=int(input("请输入Java成绩:"))
            python=int(input("请输入python成绩:"))
        except:
            print("输入无效,请重新输入~~")
            continue
        #将录入的学生信息保存到字典
        stu={"id":id,"name":name,"english":english,"java":java,"python":python}
        student_lst.append(stu)
        answer=input("是否继续添加y/n:")
        if answer=='y' or answer=='Y':
            continue
        else:
            break
    sava(student_lst)
    print("学生信息录入完毕~~")

#将键盘输入信息存入磁盘
def sava(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():
    stu_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:
                    d=dict(eval(item))
                    if id!='':
                        if d['id']==id:
                            stu_query.append(d)
                    elif name!='':
                        if d['name']==name:
                            stu_query.append(d)
            show_student(stu_query)
            stu_query.clear()
            answer=input("是否继续查询y/n:")
            if answer=='y' or answer=='Y':
                continue
            else:
                break
        else:
            print("暂未保存学生信息")
            return
#显示学生信息
def 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","姓名","英语成绩","Java成绩","python成绩","总成绩"))
    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('java'),item.get('python'),
                                 int(item.get('english'))+int(item.get('java'))+int(item.get("python"))
                                 ))
#删除学生信息
def delete():
    while True:
        stu_id=input("请输入要删除的学生ID:")
        if stu_id != "":
            if os.path.exists(filename):
                with open(filename,"r",encoding="utf_8") as file:
                    stu_old=file.readlines()
            else:
                stu_old=[]
            flag=False
            if stu_old:
                with open(filename,"w",encoding="utf-8") as wfile:
                    d={}
                    for item in stu_old:
                        d=dict(eval(item))
                        if d["id"] != stu_id:
                            wfile.write(str(d)+"\n")
                            wfile.flush()
                        else:
                            flag=True
                    wfile.close()
                    if flag:
                        print(f"id为{stu_id}的学生信息已经被删除")
                    else:
                        print(f"没有找到id为{stu_id}的学生信息")
            else:
                print("无学生信息")
                break
            show()
            answer=input("是否继续删除y/n")
            if answer=='y' or answer == 'Y':
                continue
            else:
                break
#修改学生信息
def modify():
    show()
    if os.path.exists(filename):
        with open(filename,"r",encoding="utf-8") as rfile:
            student_old=rfile.readlines()
    else:
        return
    stu_id=input("请输入你要修改的ID:")
    with open(filename,"w",encoding="utf-8") as wfile:
        for item in student_old:
            d=dict(eval(item))
            if d['id']==stu_id:
                print("找到学生信息,可以修改他的相关信息了")
                while True:
                    try:
                        d['name']=input("请输入姓名:")
                        d['english']=input("请输入英语成绩:")
                        d['java']=input("请输入Java成绩:")
                        d['python']=input("请输入python成绩")
                    except:
                        print("你的输入有误,请重新输入")
                    else:
                        break
                wfile.write(str(d)+"\n")
                print("修改成功")
            else:
                wfile.write(str(d) + "\n")
        answer=input("是否继续修改其他学生的信息:")
        if answer=='y' or answer=='Y':
            modify()
        else:
            return
#按照成绩排序
def sort():
    show()
    if os.path.exists(filename):
        with open(filename,"r",encoding="utf-8") as rfile:
            stu_lst=rfile.readlines()
        stu_new=[]
        for item in stu_lst:
            d=dict(eval(item))
            stu_new.append(d)
    else:
        return
    asc_or_desc=input("请选择 0.升序 1.降序")
    if asc_or_desc==0:
        asc_or_desc_boll=False
    elif asc_or_desc==1:
        asc_or_desc_bool = True
    else:
        print("你的输入有误,请重新输入")
        sort()
    mode=input("请选择排序方式(1.按英语成绩排序 2.按Java成绩排序 3.按python成绩排序 4.按总成绩排序):")
    if mode=="1":
        stu_new.sort(key=lambda x : int(x['english']),reverse=asc_or_desc_bool)
    elif mode=="2":
        stu_new.sort(key=lambda x: int(x['java']), reverse=asc_or_desc_bool)
    elif mode=="3":
        stu_new.sort(key=lambda x: int(x['python']), reverse=asc_or_desc_bool)
    elif mode=="4":
        stu_new.sort(key=lambda x: int(x['english']) + int(x['java']) + int(x['python']), reverse=asc_or_desc_bool)
    else:
        print("你的输入有误,请重新输入")
        sort()
    show_student(stu_lst)
#显示总成绩
def total():
    if os.path.exists(filename):
        with open(filename,"r",encoding="utf-8") as rfile:
            stu=rfile.readlines()
            if stu:
                print(f"一共有{len(stu)}名学生信息")
            else:
                print("还没有录入学生信息")
    else:
        print("暂未保存学生信息")

#显示学生信息
def show():
    stu_list=[]
    if os.path.exists(filename) :
        with open(filename,"r",encoding="utf-8") as rfile:
            students=rfile.readlines()
        for item in students:
            stu_list.append(eval(item))
        if stu_list:
            show_student(stu_list)
    else:
        print("暂未保存学生信息")

#退出
def exit():
    pass

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


#函数入口
if __name__ == '__main__':
    mainMenu()



这篇关于基于python的学生信息管理系统的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程