用Python实现学生成绩管理系统,附上完整代码

发布时间:2024年01月16日

大体功能如下,用Python语言完成的小项目,功能包括录入、查找、删除、修改、排序、显示等,提示内容有特色~可以自己修改

代码附上:

'''
作者:淳i璐
项目:学生成绩管理系统
'''
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:
                choice1 = input('主人你舍得退出系统吗^_^ (T/F):')
                if choice1 == 'T' or choice1 == 't':
                    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('输入有误,麻溜重新输入:')
    print('可恶啊,让你跑了!')


def insert():
    student_list = []
    while True:
        id = input('学生学号交上来(eg:1001):')
        if not id:
            break
        name = input('尊姓大名???:')
        if not name:
            break

        try:
            english = int(input('敢问阁下英语成绩?:'))
            chinese = int(input('敢问阁下语文成绩?:'))
            java = int(input('敢问阁下java成绩?:'))
            print('okok真棒!')
        except:
            print('请你好好输入哈,警告!')
            continue

        student = {'id':id,'name':name,'english':english,'chinese':chinese,'java':java}
        student_list.append(student)
        choice2 = input('还继续吗bro?(T/F):')
        if choice2 == 't' or choice2 == 'T':
            continue
        else:
            break
    save(student_list)

def save(lst):
    try:
        stu_txt = open(filename,'a',encoding='utf-8')
    except:
        stu_txt = open(filename,'w',encoding='utf-8')
    for i in lst:
        stu_txt.write(str(i)+'\n')
    stu_txt.close()
    print('* 嘻嘻,录入完毕!*')

def search():
    student_search = []
    while True:
        id = ''
        name = ''
        if os.path.exists(filename):
            choice = input('按id查输入1,按姓名查输入2:')
            if choice == '1':
                id = input('id啥了')
            elif choice == '2':
                name = input('大名啥了')
            else:
                print('搞错了,重来')
                search()
            with (open(filename,'r',encoding='utf-8') as rfile):
                student = rfile.readlines()
                for i in student:
                    d = dict(eval(i))
                    if id != '':
                        if d['id'] == id:
                            student_search.append(d)
                    elif name != '':
                        if d['name'] == name:
                            student_search.append(d)
                    else:
                        print('输入呀')
            show_student(student_search)
            student_search.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
    #标题显示
    print('----show time -------')
    format_title = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'
    print(format_title.format('ID','姓名','英语','语文','Java','总成绩'))
    #内容显示
    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('chinese'),
                                 item.get('java'),
                                 int(item.get('english'))+int(item.get('chinese'))+int(item.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 i in student_old:
                        d = dict(eval(i))   #字符串转为字典
                        if d['id'] != student_id:
                            wfile.write(str(d)+'\n')
                        else:
                            flag = True
                    if flag:
                        print(f'id为{student_id}的学生已经被删除')
                    else:
                        print(f'id为{student_id}的学生他也煤油啊!')
            else:
                print('不存在学生')
                break
        show()
        anwser = input('你还删吗??!(T/F)')
        if anwser == 't' or anwser == 'T':
            continue
        else:
            break



def modify():
    show()
    if os.path.exists(filename):
        with open(filename, 'r', encoding='utf-8') as file:
            student_old = file.readlines()
    else:
        student_old = []
    while True:
        flag = False

        student_id = input('请输入你要修改滴学生的ID:')
        with open(filename, 'w', encoding='utf-8') as wfile:
            d = {}
            for i in student_old:
                d = dict(eval(i))  # 字符串转为字典
                if d['id'] == student_id:
                    print('---找到这个学生了!改他!---')
                    student_name = input('叫啥呀?')
                    d['name'] = student_name
                    student_english = input('英语几分?')
                    d['english'] = student_english
                    student_chinese = input('语文几分?')
                    d['chinese'] = student_chinese
                    student_java = input('java几分?')
                    d['java'] = student_java


                    wfile.write(str(d)+'\n')
                    flag = True

                else:
                    wfile.write(str(d)+'\n')

            if flag == True:
                print(f'id为{student_id}的学生已经修改')
            else:
                print('没找到他啊')

            show()
            answer = input('还改吗?(t/f)')
            if answer == 't' or answer == 'T':
                continue
            else:
                break




def sort():
    show()
    if os.path.exists(filename):
        stu_show = []
        with open(filename,'r',encoding='utf-8') as rfile:
            stu = rfile.readlines()
            for i in stu:
                d = dict(eval(i))
                stu_show.append(d)
    else:
        print('没学生咋排序!')
        return

    choice = input('升序1,降序2:')
    if choice == '1':
        stu_bool = False
    elif choice == '2':
        stu_bool = True
    else:
        sort()
    mode = input('---0总成绩,1英语,2语文,3Java:')
    if mode == '0':
        stu_show.sort(key=lambda x:int(x['english']) + int(x['chinese']) + int(x['java']),reverse=stu_bool)
    elif mode == '1':
        stu_show.sort(key=lambda x: int(x['english']), reverse=stu_bool)

    elif mode == '2':
        stu_show.sort(key=lambda x: int(x['chinese']), reverse=stu_bool)

    elif mode == '3':
        stu_show.sort(key=lambda x: int(x['java']), reverse=stu_bool)

    else:
        sort()
    show_student(stu_show)
    answer = input('还排吗 t/f:')
    if answer == 't':
        sort()
    else:
        return



def total():
    if os.path.exists(filename):
        with open(filename, 'r', encoding='utf-8') as rfile:
            student = rfile.readlines()
            if student:
                print(f'一共有{len(student)}银')
            else:
                print('无信息')


    else:
        print('无信息')
        return
def show():
    stu_show = []
    if os.path.exists(filename):
        with open(filename,'r',encoding='utf-8') as rfile:
            stu = rfile.readlines()
            for i in stu:
                d = dict(eval(i))
                stu_show.append(d)
            if stu_show:
                show_student(stu_show)
            else:
                print('不存在')

    else:
        print('学生都跑光了')



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.退出')
    print('--------------------------------------')

if __name__ == '__main__':
    main()

简简单单,完成了入门Python的第一个小小项目,未来可期!

文章来源:https://blog.csdn.net/qq_71320435/article/details/135560503
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。