使用Python实现学生成绩管理系统

 学生成绩管理系统是一种非常普遍的管理系统

使用Python创建该系统,步骤如下:

使用os 模块 json模块,for 循环, if语句 函数,实现代码的编程,本系统主要由登录,注册,增删改查的功能 

代码如下:

import time
import os
import json

sys_data = None


def lose():
    global sys_data
    if os.path.exists("12.txt"):
        with open("12.txt", "r") as f:
            sys_data = json.load(f)
    else:
        sys_data = {
            "student": [],
            "class": [],
            "score": [],
            "users": []
        }


def save():
    with open("12.txt", "w") as f:
        json.dump(sys_data, f)


def show_menu():
    print("   请选择以下操作:    ")
    print("   1.添加学生信息     ")
    print("   2.删除学生信息     ")
    print("   3.修改学生信息     ")
    print("   4.查找学生信息     ")
    print("   5.显示所有学生     ")
    print("   6.添加课程信息     ")
    print("   7.删除课程信息     ")
    print("   8.修改课程信息     ")
    print("   9.查找课程信息     ")
    print("   10.显示所有课程    ")
    print("   11.添加学生成绩    ")
    print("   12.删除成绩信息    ")
    print("   13.修改成绩信息    ")
    print("   14.查找成绩信息    ")
    print("   15.显示成绩课程    ")
    print("    0.返回上层       ")


def add_student():
    """添加学生"""

    name = input("输入学生姓名:")
    age = input("输入学生年级:")
    sex = input("输入学生性别:")
    sys_data["student"].append({
        "sid": 101 if len(sys_data["student"]) == 0 else sys_data["student"][-1]["sid"] + 1,
        "name": name,
        "age": age,
        "sex": sex
    })

    print("添加成功", sys_data["student"])
    save()


def delete_student():
    """删除学生"""
    s = int(input("请输入删除学生的id:"))
    for i in sys_data["student"]:
        if s == i["sid"]:
            sys_data["student"].remove(i)
            print("删除成功")
            break
    else:
        print("没有找到学生")
    save()


def update_student():
    """修改"""
    s = int(input("请输入修改学生的id:"))
    for i in sys_data["student"]:
        if s == i["sid"]:
            i["name"] = input("输入修改学生姓名")
            i["age"] = input("输入修改学生年龄")
            i["sex"] = input("输入修改学生性别")
            print("修改成功")
            break
    else:
        print("没有找到学生")
    save()


def select_student():
    """查找学生"""
    s = int(input("请输入查找学生的id:"))
    for i in sys_data["student"]:
        if s == i["sid"]:
            print(i)
            break
    else:
        print("没有找到学生")


def show_all_student():
    """显示所有学生"""
    for i in sys_data["student"]:
        print(i)


def add_class():
    """添加课程"""
    name = input("输入课程:")
    sys_data["class"].append({
        "cid": 1001 if len(sys_data["class"]) == 0 else sys_data["class"][-1]["cid"] + 1,
        "name": name,
    })

    print("添加成功", sys_data["class"])
    save()


def delete_class():
    """删除课程"""
    s = int(input("请输入删除课程的id:"))
    for i in sys_data["class"]:
        if s == i["cid"]:
            sys_data["class"].remove(i)
            print("删除成功")
            break
    else:
        print("暂无课程")
    save()


def update_class():
    """修改课程"""
    s = int(input("请输入修改课程的id:"))
    for i in sys_data["class"]:
        if s == i["cid"]:
            i["name"] = input("输入修改课程名")
            print("修改成功")
            break
        else:
            print("暂无课程")
    save()


def select_class():
    """查找课程"""
    s = int(input("请输入查找课程的id:"))
    for i in sys_data["class"]:
        if s == i["cid"]:
            print(i)
            break
    else:
        print("暂无课程")


def show_all_class():
    """显示所有课程"""
    for i in sys_data["class"]:
        print(i)


def add_score():
    """添加成绩"""
    s = int(input("请输入学生id:"))
    c = int(input("请输入课程id:"))
    for i in sys_data["student"]:
        for j in sys_data["class"]:
            if s == i["sid"] and c == j["cid"]:
                a = int(input("请输入成绩"))
                sys_data["score"].append({
                    "rid": 10001 if len(sys_data["score"]) == 0 else sys_data["score"][-1]["rid"] + 1,
                    "sid": i["sid"],
                    "cid": j["cid"],
                    "result": a
                })
                print("添加成功")
                break
    else:
        print("没有找到该学生")
    save()


def delete_score():
    """删除成绩"""
    s = int(input("请输入删除学生成绩的id"))
    for i in sys_data["score"]:
        if i["rid"] == s:
            sys_data["score"].remove(i)
            print("删除成功")
            break
    else:
        print("无该学生成绩")
    save()


def update_score():
    """修改成绩"""
    s = int(input("请输入修改学生成绩的id"))
    for i in sys_data["score"]:
        if i["rid"] == s:
            i["result"] = input("请输入修改后成绩")
            print("修改成功")
            break
    else:
        print("暂无该学生信息")
    save()


def select_score():
    """查询成绩"""
    s = int(input("请输入查询学生成绩的id"))
    for i in sys_data["score"]:
        if i["rid"] == s:
            print(i)
            break
    else:
        print("暂无该生信息")


def show_all_score():
    """显示所有成绩"""
    for i in sys_data["score"]:
        print(f"学号{i['sid']} 课程编号{i['cid']} 成绩编号{i['rid']} 成绩{i['result']}")


def main():
    while True:
        show_menu()
        a = int(input("请输入数字选择操作:"))
        if a == 0:
            print("-----正在退出-----")
            time.sleep(2)
            print("-----退出成功-----")
            return
        elif a == 1:
            add_student()
        elif a == 2:
            delete_student()
        elif a == 3:
            update_student()
        elif a == 4:
            select_student()
        elif a == 5:
            show_all_student()
        elif a == 6:
            add_class()
        elif a == 7:
            delete_class()
        elif a == 8:
            update_class()
        elif a == 9:
            select_class()
        elif a == 10:
            show_all_class()
        elif a == 11:
            add_score()
        elif a == 12:
            delete_score()
        elif a == 13:
            update_score()
        elif a == 14:
            select_score()
        elif a == 15:
            show_all_score()
        else:
            print("请输入正确数字")


def logon():
    """注册"""
    a = input("请输入账号:")
    a1 = int(input("请输入密码:"))
    a2 = int(input("请确认密码:"))
    for i in sys_data["users"]:
        if a == i["number"]:
            print("此账号已存在")
            break
    else:
        if a1 == a2:
            sys_data["users"].append({
                "number": a,
                "cipher": a1
            })
            print("注册成功")
        else:
            print("密码不一致,注册失败")
    save()


def sg():
    """登录"""
    a = input("请输入账号:")
    a1 = int(input("请输入密码:"))
    for i in sys_data["users"]:
        if i["number"] != a:
            print("此账号不存在")
        else:
            if i["cipher"] == a1:
                print("-----正在登录-----")
                time.sleep(2)
                print("-----登录成功-----")
                main()
            else:
                print("密码错误")
                break


def sign():
    while True:
        print("请选择以下操作:")
        print("1.登录")
        print("2.注册")
        print("3.退出登录")
        a = input("请选择操作:")
        if a == "1":
            sg()
        elif a == "2":
            logon()
        elif a == "3":
            print("-----正在退出-----")
            time.sleep(2)
            print("-----退出成功-----")
            return
        else:
            print("请重新输入")


lose()
sign()

希望对大家有所帮!

你可能感兴趣的:(python,json,机器学习)