目 录CONTENT

文章目录

学生管理系统

懿曲折扇情
2022-05-06 / 0 评论 / 2 点赞 / 153 阅读 / 5,660 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-05-07,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。
广告 广告

一、学生管理系统

studentsystem.py
# coding=utf-8
"""
    作者:gaojs
    功能:
    新增功能:
    日期:2022/4/3 23:41
"""
import os.path


filename = 'student.txt'


def main():
    """
    学生管理系统主函数
    :return:
    """
    while True:
        num()
        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()
            elif choice == 7:
                show()


def num():
    """
    打印功能列表函数
    :return:
    """
    print('========================学生成绩管理系统========================')
    print('---------------------------功能菜单----------------------------')
    print('\t\t\t\t\t\t1.录入学生系统')
    print('\t\t\t\t\t\t2.查找学生系统')
    print('\t\t\t\t\t\t3.删除学生系统')
    print('\t\t\t\t\t\t4.修改学生系统')
    print('\t\t\t\t\t\t5.排序')
    print('\t\t\t\t\t\t6.统计学生总人数')
    print('\t\t\t\t\t\t7.显示所有学生信息')
    print('\t\t\t\t\t\t0.退出')


def insert():
    """
    1.录入学生信息
    :return:
    """
    student_list = []
    while True:
        id = input('请输入ID(如1001):')
        if not id:
            break
        name = input('请输入姓名:')
        if not name:
            break

        try:
            english = int(input('请输入英语成绩:'))
            python = int(input('请输入python成绩:'))
            java = int(input('请输入java成绩:'))
        except Exception as e:
            print(e)
            print('输入有误,不是整数类型,请重新输入!')
            continue
        # 将录入的系统保存到字典
        student = {'id': id, 'name': name, 'english': english, 'python': python, 'java': java}
        # 将学生信息添加到列表中
        student_list.append(student)
        answer = input('是否继续添加y/n\n')
        if answer == 'y' or answer == 'Y':
            continue
        else:
            break
        # 保存
    save(student_list)
    print('学生信息录入完毕!!!')


def save(lst):
    """
    保存学生信息
    :param lst:
    :return:
    """
    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():
    """
    2.查找学生数据
    :return:
    """
    student_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 f:
                student = f.readlines()
                for item in student:
                    d = dict(eval(item))
                    if d['id'] == id:
                        student_query.append(d)
                    elif d['name'] == name:
                        student_query.append(d)

            # 显示查询结果
            show_student(student_query)
            # 清空列表
            student_query.clear()
            answer = input('是否要继续查询?y/n\n')
            if answer == 'y':
                continue
            else:
                break
        else:
            print('暂未保存学生信息!')
            return


def show_student(lst):
    """
    显示学生信息
    :param lst:
    :return:
    """
    if len(lst) == 0:
        print('没有查询到学生信息,无数据显示!!!')
        return
    # 定义标题显示
    format_tltle = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'
    print(format_tltle.format('ID', '姓名', '英语成绩', 'python成绩', 'java成绩', '总成绩'))
    # 定义内容的显示格式
    format_data = '{:^6}\t{:^12}\t{:^10}\t{:^10}\t{:^16}\t{:^12}'
    for item in lst:
         print(format_data.format(item.get('id'),
                                  item.get('name'),
                                  item.get('english'),
                                  item.get('python'),
                                  item.get('java'),
                                  int(item.get('english')) + int(item.get('python')) + int(item.get('java'))
                                  ))


def delete():
    """
    3.删除学生信息
    :return:
    """
    while True:
        student_id = input('请输入要删除学生的ID:')
        if student_id != '':
            if os.path.exists(filename):
                with open(filename, 'r', encoding='utf-8') as f:
                    student_old = f.readlines()
            else:
                student_old = []
            flag = False
            if student_old:
                with open(filename, 'w', encoding='utf-8') as f:
                    for item in student_old:
                        d = dict(eval(item))# 将字符串转为字典
                        if d['id'] != student_id:
                            f.write(str(d) + '\n')
                        else:
                            flag = True
                    if flag:
                        print(f'id为{student_id}的学生信息已被删除')
                    else:
                        print(f'没有找到ID为{student_id}的学生信息')
            else:
                print('无学生信息')
                break
            show()
            answer = input('是否继续删除?y/n\n')
            if answer == 'y' or answer == 'Y':
                continue
            else:
                break


def modify():
    """
    4.修改学生信息
    :return:
    """
    show()
    if os.path.exists(filename):
        with open(filename, 'r', encoding='utf-8') as f1:
            student_old = f1.readlines()

    else:
        return
    student_id = input('请输入学生的ID:')
    with open(filename, 'w', encoding='utf-8') as f:
        for item in student_old:
            d = dict(eval(item))
            if d['id'] == student_id:
                print('找到学生信息,可以修改相关信息!')
                while True:
                    try:
                        d['name'] = input('请输入姓名:')
                        d['english'] = input('请输入英语成绩:')
                        d['python'] = input('请输入python成绩:')
                        d['java'] = input('请输入java成绩:')
                    except Exception as e:
                        print(e)
                        print('您输入的有误,请重新输入:')
                    else:
                        break

                f.write(str(d) + '\n')
                print('修改成功')
            else:
                f.write(str(d) + '\n')
                print('没找到学生信息,请重试!')
        answer = input('是否继续修改其他学生信息?y/n\n')
        if answer == 'y' or answer == 'Y':
            modify()


def sort():
    """
    排序
    :return:
    """
    pass


def total():
    """
    6.统计所有学生信息个数:6
    :return:
    """
    if os.path.exists(filename):
        with open(filename, 'r', encoding='utf-8') as f:
            students= f.readlines()
            if students:
                print(f'一共有{len(students)}个学生信息!')
            else:
                print('还没有录入学生信息!')
    else:
        print('暂未保存数据!!!')


def show():
    """
    7.显示所有学生信息
    :return:
    """
    student_list = []
    if os.path.exists(filename):
        with open(filename, 'r', encoding='utf-8') as f:
            students = f.readlines()
            for item in students:
                student_list.append(eval(item))
            if student_list:
                show_student(student_list)
    else:
        print('暂未保存过数据!')


if __name__ == '__main__':
    main()

2

评论区