python 学生成绩管理系统

"""
用户
学生
课程
成绩
"""
import os
import json

FILE_PATH = "./system.json"

system_data = {
    "users": [],
    "students": [],
    "courses": [],
    "scores": []
}


def show_main_menu():
    main_menu = """0. 返回登录界面
1. 添加学生
2. 删除学生
3. 修改学生信息
4. 查看指定学生详细信息
5. 查看所有学生列表
6. 添加课程
7. 删除课程
8. 修改课程
9. 查看指定课程详细信息
10. 查看所有课程信息
11. 添加成绩
12. 删除成绩
13. 修改成绩
14. 查看指定成绩详细信息
15. 查看所有成绩信息
16. 查看所有不及格的成绩信息"""
    print(main_menu)


def load_data():
    global system_data
    if os.path.exists(FILE_PATH):
        with open(FILE_PATH, "r", encoding="utf8") as f:
            system_data = json.load(f)


def save_data():
    with open(FILE_PATH, "w", encoding="utf8") as f:
        json.dump(system_data, f, ensure_ascii=False)


def get_user_main_option():
    while True:
        option = input("输入数字选择对应的操作")
        if option not in [str(i) for i in range(17)]:
            print(f"输入错误,请输入选项对应的数字")
        else:
            return option


def get_user_sub_option():
    while True:
        sub_option = input("请输入对应的选项: 继续(Y) 返回(N)")
        if sub_option not in ["Y", "y", "N", "n"]:
            print(f"输入不合法,只能输入Y/N")
        else:
            return sub_option


def get_student_name():
    while True:
        student_name = input("请输入学生名字")
        if 2 <= len(student_name) <= 4:
            return student_name
        else:
            print(f"输入不合法,名字长度2-4")


def get_student_age():
    while True:
        student_age = int(input("请输入学生年纪"))
        if 15 <= student_age <= 25:
            return student_age
        else:
            print(f"输入不合法,年纪范围15-25")


def get_student_sex():
    while True:
        student_sex = input("请输入学生性别")
        if student_sex in ["男", "女"]:
            return student_sex
        else:
            print(f"输入不合法,只能输入男/女")


def add_student():
    while True:
        student_name = get_student_name()
        student_age = get_student_age()
        student_sex = get_student_sex()
        system_data["students"].append({
            "id": 101 if not system_data["students"] else system_data["students"][-1]["id"] + 1,
            "name": student_name,
            "age": student_age,
            "sex": student_sex
        })
        save_data()
        print(f"添加学生成功, 现有学生 {len(system_data['students'])} 人")
        sub_option = get_user_sub_option()
        if sub_option == "Y" or sub_option == "y":
            pass
        else:
            break


def remove_student():
    while True:
        sid = int(input("输入将要删除学生的id"))
        for s in system_data["students"]:
            if s["id"] == sid:
                system_data["students"].remove(s)
                save_data()
                print(f"删除学生成功,现有学生 {len(system_data['students'])} 人")
                break
        else:
            print(f"没有找到id为{sid}的学生")

        sub_option = get_user_sub_option()
        if sub_option == "Y" or sub_option == "y":
            pass
        else:
            break


def update_student():
    while True:
        sid = int(input("输入将要修改学生的id"))
        for s in system_data["students"]:
            if s["id"] == sid:
                s["name"] = get_student_name()
                s["age"] = get_student_age()
                s["sex"] = get_student_sex()
                save_data()
                print(f"修改学生成功")
                break
        else:
            print(f"没有找到id为{sid}的学生")
        sub_option = get_user_sub_option()
        if sub_option == "Y" or sub_option == "y":
            pass
        else:
            break


def get_student_by_id(sid):
    for s in system_data["students"]:
        if s["id"] == sid:
            return s


def show_student_by_id():
    while True:
        sid = int(input("输入将要查看学生的id"))
        s = get_student_by_id(sid)
        if s:
            print(f"id:{s['id']}\tname:{s['name']}\tage:{s['age']}\tsex:{s['sex']}")
        else:
            print(f"没有找到对应的学生")
        sub_option = get_user_sub_option()
        if sub_option == "Y" or sub_option == "y":
            pass
        else:
            break


def show_all_students():
    if system_data["students"]:
        for s in system_data["students"]:
            print(f"id:{s['id']}\tname:{s['name']}\tage:{s['age']}\tsex:{s['sex']}")
    else:
        print(f"还没有添加学生")


def get_course_name():
    while True:
        course_name = input("请输入课程名字")
        if 3 <= len(course_name) <= 10:
            return course_name
        else:
            print(f"输入不合法,名字长度3-10")


def add_course():
    while True:
        course_name = get_course_name()
        system_data["courses"].append({
            "id": 1001 if not system_data["courses"] else system_data["courses"][-1]["id"] + 1,
            "name": course_name
        })
        save_data()
        print(f"添加课程成功, 现有课程 {len(system_data['courses'])} 门")
        sub_option = get_user_sub_option()
        if sub_option == "Y" or sub_option == "y":
            pass
        else:
            break


def remove_course():
    while True:
        cid = int(input("输入将要删除课程的id"))
        for c in system_data["courses"]:
            if c["id"] == cid:
                system_data["courses"].remove(c)
                save_data()
                print(f"删除课程成功,现有课程 {len(system_data['courses'])}  门")
                break
        else:
            print(f"没有找到id为{cid}的课程")

        sub_option = get_user_sub_option()
        if sub_option == "Y" or sub_option == "y":
            pass
        else:
            break


def update_course():
    while True:
        cid = int(input("输入将要修改课程的id"))
        for c in system_data["courses"]:
            if c["id"] == cid:
                c["name"] = get_course_name()
                save_data()
                print(f"修改课程成功")
                break
        else:
            print(f"没有找到id为{cid}的课程")
        sub_option = get_user_sub_option()
        if sub_option == "Y" or sub_option == "y":
            pass
        else:
            break


def get_course_by_id(cid):
    for c in system_data["courses"]:
        if c["id"] == cid:
            return c


def show_all_courses():
    if system_data["courses"]:
        for c in system_data["courses"]:
            print(f"id:{c['id']}\tname:{c['name']}")
    else:
        print(f"还没有添加课程")


def show_course_by_id():
    while True:
        cid = int(input("输入将要查看课程的id"))
        c = get_course_by_id(cid)
        if c:
            print(f"id:{c['id']}\tname:{c['name']}")
        else:
            print(f"没有找到对应的课程")
        sub_option = get_user_sub_option()
        if sub_option == "Y" or sub_option == "y":
            pass
        else:
            break


def get_score():
    while True:
        score = int(input("请输入成绩"))
        if 0 <= score <= 100:
            return score
        else:
            print(f"输入不合法,成绩范围0-100")


def add_score():
    while True:
        while True:
            sid = int(input("输入学生id"))
            s = get_student_by_id(sid)
            if s:
                break
            else:
                print(f"没有找到id为{sid}的学生")
        while True:
            cid = int(input("输入课程id"))
            c = get_course_by_id(cid)
            if c:
                break
            else:
                print(f"没有找到id为{cid}的课程")
        for score in system_data["scores"]:
            if score["sid"] == sid and score["cid"] == cid:
                print(f"学生{s['name']}的课程{c['name']}已经存在")
                break
        else:
            system_data["scores"].append({
                "id": 10001 if not system_data["scores"] else system_data["scores"][-1]["id"] + 1,
                "sid": sid,
                "cid": cid,
                "score": get_score()
            })
            print(f"添加成绩成功")
            save_data()
        sub_option = get_user_sub_option()
        if sub_option == "Y" or sub_option == "y":
            pass
        else:
            break


def remove_score():
    while True:
        while True:
            sid = int(input("输入学生id"))
            s = get_student_by_id(sid)
            if s:
                break
            else:
                print(f"没有找到id为{sid}的学生")
        while True:
            cid = int(input("输入课程id"))
            c = get_course_by_id(cid)
            if c:
                break
            else:
                print(f"没有找到id为{cid}的课程")
        for score in system_data["scores"]:
            if score["sid"] == sid and score["cid"] == cid:
                system_data["scores"].remove(score)
                print(f"删除成绩成功")
                save_data()
                break
        else:
            print(f"没有找到学生{s['name']} 课程{c['name']}的成绩")
        sub_option = get_user_sub_option()
        if sub_option == "Y" or sub_option == "y":
            pass
        else:
            break


def update_score():
    while True:
        while True:
            sid = int(input("输入学生id"))
            s = get_student_by_id(sid)
            if s:
                break
            else:
                print(f"没有找到id为{sid}的学生")
        while True:
            cid = int(input("输入课程id"))
            c = get_course_by_id(cid)
            if c:
                break
            else:
                print(f"没有找到id为{cid}的课程")
        for score in system_data["scores"]:
            if score["sid"] == sid and score["cid"] == cid:
                score["score"] = get_score()
                save_data()
                print(f"修改成功")
                break
        else:
            print(f"没有找到学生{s['name']} 课程{c['name']}的成绩")
        sub_option = get_user_sub_option()
        if sub_option == "Y" or sub_option == "y":
            pass
        else:
            break


def show_score_by_id():
    while True:
        while True:
            sid = int(input("输入学生id"))
            s = get_student_by_id(sid)
            if s:
                break
            else:
                print(f"没有找到id为{sid}的学生")
        while True:
            cid = int(input("输入课程id"))
            c = get_course_by_id(cid)
            if c:
                break
            else:
                print(f"没有找到id为{cid}的课程")
        for score in system_data["scores"]:
            if score["sid"] == sid and score["cid"] == cid:
                print(f"id:{score['id']}\t 学生名:{s['name']}\t 课程名{c['name']}\t 成绩:{score['score']}")
                break
        else:
            print(f"没有找到学生{s['name']} 课程{c['name']}的成绩")
        sub_option = get_user_sub_option()
        if sub_option == "Y" or sub_option == "y":
            pass
        else:
            break


def show_all_scores():
    if system_data["scores"]:
        for score in system_data["scores"]:
            s = get_student_by_id(score["sid"])
            c = get_course_by_id(score["cid"])
            print(f"id:{score['id']}\t 学生名:{s['name']}\t 课程名{c['name']}\t 成绩:{score['score']}")
    else:
        print(f"还没有添加成绩信息")


def show_all_scores_down_60():
    if system_data["scores"]:
        for score in system_data["scores"]:
            if score['score'] < 60:
                s = get_student_by_id(score["sid"])
                c = get_course_by_id(score["cid"])
                print(f"id:{score['id']}\t 学生名:{s['name']}\t 课程名{c['name']}\t 成绩:{score['score']}")
    else:
        print(f"还没有添加成绩信息")


def main():
    while True:
        show_main_menu()
        option = get_user_main_option()
        if option == "0":
            break
        elif option == "1":
            add_student()
        elif option == "2":
            remove_student()
        elif option == "3":
            update_student()
        elif option == "4":
            show_student_by_id()
        elif option == "5":
            show_all_students()
        elif option == "6":
            add_course()
        elif option == "7":
            remove_course()
        elif option == "8":
            update_course()
        elif option == "9":
            show_course_by_id()
        elif option == "10":
            show_all_courses()
        elif option == "11":
            add_score()
        elif option == "12":
            remove_score()
        elif option == "13":
            update_score()
        elif option == "14":
            show_score_by_id()
        elif option == "15":
            show_all_scores()
        elif option == "16":
            show_all_scores_down_60()


# main()


def get_user_name():
    while True:
        user_name = input("请输入用户名")
        if 2 <= len(user_name) <= 10:
            return user_name
        else:
            print(f"输入不合法,名字长度2-10")


def get_user_password(info):
    while True:
        user_password = input(info)
        if 2 <= len(user_password) <= 10:
            return user_password
        else:
            print(f"输入不合法,名字长度2-10")


def login():
    while True:
        has_user_name = False
        user_name = get_user_name()
        for user in system_data["users"]:
            if user["username"] == user_name:
                has_user_name = True
                break
        else:
            print(f"用户名不存在,请从新输入")
        if has_user_name:
            break

    while True:
        can_login = False
        user_password = get_user_password("请输入用户密码")
        for user in system_data["users"]:
            if user["username"] == user_name and user["password"] == user_password:
                print(f"登录成功")
                can_login = True
                break
        else:
            print(f"密码错误,请从新输入")

        if can_login:
            break

    main()


def regist():
    while True:
        user_name = get_user_name()
        for user in system_data["users"]:
            if user["username"] == user_name:
                print(f"用户名已经存在,请从新输入")
                break
        else:
            break
    while True:
        user_password = get_user_password("请输入用户密码")
        user_password2 = get_user_password("请再次输入用户密码")
        if user_password != user_password2:
            print(f"密码不一致,请从新输入")
        else:
            break
    system_data["users"].append({
        "id": 11 if not system_data["users"] else system_data["users"][-1]["id"] + 1,
        "username": user_name,
        "password": user_password
    })
    save_data()
    print(f"注册成功")


def user_login_regist():
    load_data()
    while True:
        menu = """0. 退出程序
1. 登录
2. 注册"""
        print(menu)
        while True:
            option = input("输入数字,选择对应的选项")
            if option not in [str(i) for i in range(3)]:
                print(f"输入错误,请输入对应的数字")
            else:
                break
        if option == "0":
            break
        elif option == "1":
            login()
        elif option == "2":
            regist()


user_login_regist()

python 学生成绩管理系统 完整版

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农NoError

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值