了解函数到学生成绩管理系统函数版

def login_required(f):
    def check():
        name = input("请输入名字")
        pwd = input("请输入密码")
        if name == "lhj" and pwd == "123456":
            f()
        else:
            print("密码错误")

    return check


datas = {
    "students": [{"id": 101, "name": "马云", "age": 19, "sex": "男"},
                 {"id": 102, "name": "王师", "age": 21, "sex": "男"}],
    "course": [{"id": 1001, "name": "python"}],
    "score": [{"id": 10001, "sid": 101, "cid": 1001, "score": 90},
              {"id": 10001, "sid": 102, "cid": 1001, "score": 89}]
}


def display_sub_menu(option_name):
    print("选择操作".center(30, "*"))
    print("0. 返回主菜单")
    print(f"1. {option_name}")
    print()


def display_main_menu():
    print("选择操作".center(30, "*"))
    print("0. 退出程序")
    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()


def check_name():
    while True:
        name = input("输入学生姓名")
        if 2 <= len(name) <= 4:
            return name
        else:
            print("名字不合法(长度位于2-4), 请继续输入")


def check_age():
    while True:
        age = input("输入学生年纪")
        age = int(age)
        if 18 <= age <= 25:
            return age
        else:
            print("年纪不合法(区间18-25), 请继续输入")


def check_sex():
    while True:
        sex = input("输入学生性别")
        if sex in ["男", "女", "保密"]:
            return sex
        else:
            print("性别不合法(男/女/保密), 请继续输入")


def check_input():
    option = input("输入数字".center(30, "*"))
    if option not in ["0", "1"]:
        print("输入不合法")
    else:
        if option == "0":
            return False
        else:
            return True


def add_student():
    while True:
        display_sub_menu("添加学生")
        if not check_input():
            break
        else:
            name = check_name()
            age = check_age()
            sex = check_sex()

            current_id = 101 if not datas["students"] else datas["students"][-1]["id"] + 1
            datas["students"].append({
                "name": name,
                "age": age,
                "sex": sex,
                "id": current_id
            })
            print("添加学生成功", datas["students"])


def delete_student():
    while True:
        display_sub_menu("删除学生")
        if not check_input():
            break
        else:
            sid = input("输入学生id")
            sid = int(sid)
            for s in datas["students"]:
                if s["id"] == sid:
                    datas["students"].remove(s)
                    print("删除成功", datas["students"])
                    break
            else:
                print("没有找到该学生")


def update_student():
    while True:
        display_sub_menu("更新学生信息")
        if not check_input():
            break
        else:
            sid = input("输入学生id")
            sid = int(sid)
            for s in datas["students"]:
                if s["id"] == sid:
                    name = check_name()
                    age = check_age()
                    sex = check_sex()
                    s.update({
                        "name": name,
                        "age": age,
                        "sex": sex
                    })
                    print("更新学生信息成功", datas["students"])
                    break
            else:
                print("没有找到该学生")


def find_student():
    while True:
        display_sub_menu("查看指定学生信息")
        if not check_input():
            break
        else:
            sid = input("输入学生id")
            sid = int(sid)
            for s in datas["students"]:
                if s["id"] == sid:
                    print(f"学生: ID:{s['id']}\tName:{s['name']}\tAge:{s['age']}\tSex:{s['sex']}")
                    break
            else:
                print("没有找到该学生")


def show_all_student():
    if not datas["students"]:
        print("暂时没有学生信息")
    else:
        for s in datas["students"]:
            print(f"学生: ID:{s['id']}\tName:{s['name']}\tAge:{s['age']}\tSex:{s['sex']}")


def check_course_name():
    while True:
        course_name = input("输入课程名字")
        if 10 >= len(course_name) >= 3:
            break
        else:
            print("课程名不合法, 长度[3, 10]")
    return course_name


def add_course():
    while True:
        display_sub_menu("添加课程")
        if not check_input():
            break
        else:
            course_name = check_course_name()
            for course in datas["course"]:
                if course["name"] == course_name:
                    print("课程名字已经存在")
                    break
            else:
                datas["course"].append({
                    "id": 1001 if not datas["course"] else datas["course"][-1]["id"] + 1,
                    "name": course_name
                })
                print("课程添加成功", datas["course"])


def update_course():
    while True:
        display_sub_menu("修改课程")
        if not check_input():
            break
        else:
            cid = input("输入课程id")
            cid = int(cid)
            for c in datas["course"]:
                if c["id"] == cid:
                    name = check_course_name()
                    c.update({
                        "name": name,
                    })
                    print("更新课程信息成功", datas["course"])
                    break
            else:
                print("没有找到该课程")


def delete_course():
    while True:
        display_sub_menu("删除课程")
        if not check_input():
            break
        else:
            cid = input("输入课程id")
            cid = int(cid)
            for c in datas["course"]:
                if c["id"] == cid:
                    datas["course"].remove(c)
                    print("删除成功", datas["course"])
                    break
            else:
                print("没有找到该课程")


def find_course():
    while True:
        display_sub_menu("查看指定课程信息")
        if not check_input():
            break
        else:
            cid = input("输入课程id")
            cid = int(cid)
            for c in datas["course"]:
                if c["id"] == cid:
                    print(f"课程: ID:{c['id']}\tName:{c['name']}\t")
                    break
            else:
                print("没有找到该课程")


def show_all_course():
    if not datas["course"]:
        print("暂时没有课程信息")
    else:
        for c in datas["course"]:
            print(f"课程: ID:{c['id']}\tName:{c['name']}\t")


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


def add_score():
    while True:
        display_sub_menu("添加分数")
        if not check_input():
            break
        else:
            score = check_score()
            datas["score"].append({
                "id": 10001 if not datas["score"] else datas["score"][-1]["id"] + 1,
                "sid": 101 if not datas["score"] else datas["score"][-1]["id"] + 1,
                "cid": 1001 if not datas["score"] else datas["score"][-1]["id"] + 1,
                "score": score
            })
            print("添加分数成功", datas["score"])


def delete_score():
    while True:
        display_sub_menu("删除分数")
        if not check_input():
            break
        else:
            id = input("输入分数id")
            id = int(id)
            for s in datas["score"]:
                if s["id"] == id:
                    datas["score"].remove(s)
                    print("删除成功", datas["score"])
                    break
            else:
                print("没有找到该分数")


def update_score():
    while True:
        display_sub_menu("修改分数")
        if not check_input():
            break
        else:
            id = input("输入分数id")
            id = int(id)
            for s in datas["score"]:
                if s["id"] == id:
                    score = check_score()
                    s.update({
                        "score": score,
                    })
                    print("修改分数成功", datas["score"])
                    break
            else:
                print("没有找到该分数")


def find_score():
    while True:
        display_sub_menu("查看个人分数")
        if not check_input():
            break
        else:
            sid = input("输入学生id")
            sid = int(sid)
            for s in datas["score"]:
                if s["sid"] == sid:
                    print(f"分数: ID:{s['id']}\tsid:{s['sid']}\tcid:{s['cid']}\tscore:{s['score']}\t")
                    break
            else:
                print("没有找到该学生")


def show_all_score():
    if not datas["score"]:
        print("暂时没有学生分数")
    else:
        for s in datas["score"]:
            print(f"分数: ID:{s['id']}\tsid:{s['sid']}\tcid:{s['cid']}\tscore:{s['score']}\t")


@login_required
def main():
    print("欢迎到来学生管理系统".center(30, "*"))
    while True:
        display_main_menu()
        option = input("输入数字".center(30, "*"))
        if option not in [str(i) for i in range(16)]:
            print("输入不合法")
        else:
            if option == "0":
                break
            elif option == "1":
                add_student()
            elif option == "2":
                delete_student()
            elif option == "3":
                update_student()
            elif option == "4":
                find_student()
            elif option == "5":
                show_all_student()
            elif option == "6":
                add_course()
            elif option == "7":
                delete_course()
            elif option == "8":
                update_course()
            elif option == "9":
                find_course()
            elif option == "10":
                show_all_course()
            elif option == "11":
                add_score()
            elif option == "12":
                delete_score()
            elif option == "13":
                update_score()
            elif option == "14":
                find_score()
            elif option == "15":
                show_all_score()

            else:
                print(f"你选择的是{option}")


main()

学生成绩管理系统进阶函数版

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值