python实现简单的学生成绩管理系统

 更新时间:2022年02月25日 14:50:58   作者:张登辉的奇妙旅程  
这篇文章主要为大家详细介绍了python实现简单的学生成绩管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了python实现学生成绩管理系统的具体代码,供大家参考,具体内容如下

需求:

代码:

import os
filename = 'student.txt'

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("谢谢使用!")
                    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()
            else:
                show()
# 菜单
def menu():
    print("====================学生成绩管理系统=========================")
    print("======================功能菜单==============================")
    print("\t\t1、录入学生信息")
    print("\t\t2、查找学生信息")
    print("\t\t3、删除学生信息")
    print("\t\t4、修改学生信息")
    print("\t\t5、排序")
    print("\t\t6、统计学生人数")
    print("\t\t7、显示所有学生信息")
    print("\t\t0、退出系统")
    print("--------------------------------------------------------------")
# 插入
def insert():
    student_list=[]
    while True:
        id = input('请输入ID(如1001):')
        if not id:
            break
        name = input('请输入姓名:')
        if not name:
            break

        try:
            english=int(input("input english grade:"))
            python=int(input("input python grade:"))
            java=int(input("input java grade:"))
        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':
            continue
        else:
            break

    # 调用save函数将其保存在文本当中
    save(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():
    while True:
        student_name = input("请输入你要查找的学生姓名:")
        if student_name:
            if os.path.exists(filename):
                with open(filename,'r',encoding='utf-8') as rfile:
                    student_old = rfile.readlines()
            else:
                student_old = []

            if student_old:
                d = {}
                flag = False
                for item in student_old:
                    d = dict(eval(item))
                    if d['name'] == student_name:
                        flag = True
                        student_show(d)
                    else:
                        pass
                    
            else:
                print("无学生信息,请添加!")
            if flag:
                print("查询成功")
            else:
                print("查询失败")
        else:
            print("没有输入学生姓名!")
        
        answer = input("请问是否继续查询:y/n")
        if answer == "y":
            continue
        else:
            break
# 展示个人信息
def student_show(dic):
    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}'
    print(format_data.format(dic.get('id'),
                            dic.get('name'),
                            dic.get('english'),
                            dic.get('python'),
                            dic.get('java'),
                            int(dic.get('english'))+int(dic.get('python'))+int(dic.get('java')),
                            ))
# 删除
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 = 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
        else:
            print("请输入要删除的ID:")
            continue
# 修改      
def modify():
    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=[]
            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:
                            answer = input('已经找到该学生信息,是否修改:y/n?')
                            flag = False
                            if answer:
                                d['name']=input("姓名:")
                                d['english']=int(input("english:"))
                                d['python']=int(input("python:"))
                                d['java']=int(input("java:"))
                                wfile.write(str(d)+"\n")
                                flag = True
                            else:
                                wfile.write(str(d)+"\n")
                                break
                        else:
                            wfile.write(str(d)+"\n")
                    if flag:
                        print("修改成功!")
                    else:
                        print("修改失败!")
            else:
                print("无学生信息,请添加")
            answer = input("请问是否继续修改?y/n")
            if answer == "y":
                continue
            else:
                break
        else:
            print("输入错误!")
# 排序           
def sort():
    while True:
        if os.path.exists(filename):
            with open(filename,'r',encoding='utf-8') as file:
                student_old = file.readlines()
            student_new = []
            d={}
            for item in student_old:
                d=dict(eval(item))
                student_new.append(d)
            asc_and_desc = int(input("怎么排序:0 升序  1 降序:"))
            choice = int(input("按照什么排序:1 english  2 python  3 java  0 总成绩:"))
            if choice == 1:
                student_new.sort(key=lambda x:int(x['english']),reverse=asc_and_desc)
            elif choice == 2:
                student_new.sort(key=lambda x:int(x['python']),reverse=asc_and_desc)
            elif choice == 3:  
                student_new.sort(key=lambda x:int(x['java']),reverse=asc_and_desc)
            elif choice == 0:
                student_new.sort(key=lambda x:int(x['english'])+int(x['python'])+int(x['java']),reverse=asc_and_desc)
            else:
                print("输入有误")
                sort()
        else:
            print("无学生信息")
        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 dic in student_new:
            print(format_data.format(dic.get('id'),
                                    dic.get('name'),
                                    dic.get('english'),
                                    dic.get('python'),
                                    dic.get('java'),
                                    int(dic.get('english'))+int(dic.get('python'))+int(dic.get('java')),
                                    ))
        answer = input("请问是否继续排序?y/n")
        if answer == "y":
            continue
        else:
            break
# 总人数
def total():
    if os.path.exists(filename):
        with open(filename,'r',encoding='utf-8') as file:
            student_old = file.readlines()
        print('一共有',len(student_old),'名学生!')
    else:
        print("无人员资料")
    import msvcrt
    msvcrt.getch()
# 展示
def show():
    if os.path.exists(filename):
        with open(filename,'r',encoding='utf-8') as file:
            student_old = file.readlines()
        d = {}
        students = []
        for item in student_old:
            d = dict(eval(item))
            students.append(d)
    else:
        print("没有信息!")
    if len(students):
        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 dic in students:
            print(format_data.format(dic.get('id'),
                                    dic.get('name'),
                                    dic.get('english'),
                                    dic.get('python'),
                                    dic.get('java'),
                                    int(dic.get('english'))+int(dic.get('python'))+int(dic.get('java')),
                                    ))
    else:
        print("无人员信息!")
    import msvcrt
    msvcrt.getch()

if __name__ == '__main__':
    main()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Python实现斐波那契数列的示例代码

    Python实现斐波那契数列的示例代码

    斐波那契数列是一种经典的数学问题,在计算机科学和编程中经常被用来演示算法和递归的概念,本文将详细介绍斐波那契数列的定义、计算方法以及如何在Python中实现它,需要的可以参考下
    2024-01-01
  • 解读pandas交叉表与透视表pd.crosstab()和pd.pivot_table()函数

    解读pandas交叉表与透视表pd.crosstab()和pd.pivot_table()函数

    这篇文章主要介绍了pandas交叉表与透视表pd.crosstab()和pd.pivot_table()函数的用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09
  • python argparse传入布尔参数false不生效的解决

    python argparse传入布尔参数false不生效的解决

    这篇文章主要介绍了python argparse传入布尔参数false不生效的解决,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-04-04
  • Python字典“键”和“值”的排序5种方法

    Python字典“键”和“值”的排序5种方法

    这篇文章主要介绍了5种Python字典“键”和“值”的排序方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-03-03
  • Python回溯法(Backtracking)的具体使用

    Python回溯法(Backtracking)的具体使用

    在Python中,我们可以应用回溯法解决各种问题,如八皇后问题、子集问题等,本文就来介绍一下Python回溯法(Backtracking)的具体使用,感兴趣的可以了解一下
    2023-12-12
  • window7下的python2.7版本和python3.5版本的opencv-python安装过程

    window7下的python2.7版本和python3.5版本的opencv-python安装过程

    这篇文章主要介绍了window7下的python2.7版本和python3.5版本的opencv-python安装过程,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-10-10
  • matplotlib grid()设置网格线外观的实现

    matplotlib grid()设置网格线外观的实现

    这篇文章主要介绍了matplotlib grid()设置网格线外观的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • Python中Numba库装饰器的具体使用

    Python中Numba库装饰器的具体使用

    Numba是一个针对Python的开源JIT编译器,使用Numba非常方便,只需要在Python原生函数上增加一个装饰器,本文主要介绍了Python中Numba库装饰器的具体使用,感兴趣的可以了解一下
    2024-01-01
  • Python django搭建layui提交表单,表格,图标的实例

    Python django搭建layui提交表单,表格,图标的实例

    今天小编就为大家分享一篇Python django搭建layui提交表单,表格,图标的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-11-11
  • Python Matplotlib库实现画局部图

    Python Matplotlib库实现画局部图

    这篇文章主要为大家详细介绍了Python Matplotlib库实现画局部图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-11-11

最新评论