day11_ task_学生管理系统Task

要求: 请编写函数实现学生管理系统


def founc1():
    import json
    with open('number.json', 'r', encoding='utf-8') as f:
        content = f.read()
    number = json.loads(content)
    with open('stu.json', 'r', encoding='utf-8') as f:
        content = f.read()
    all_student = json.loads(content, encoding='utf-8')
    while True:
        print("你好!欢迎")
        print("1. 添加学生")
        print("2. 查看学生")
        print("3. 修改学生信息")
        print("4. 删除学生")
        print("5. 返回")
        print("=====================================")
        select = int(input("请选择(1-5):"))
        if select == 1:
            while True:
                stu_dict = {}
                number += 1
                name = input("请输入学生姓名:")
                stu_dict["name"] = name
                age = int(input("请输入学生年龄:"))
                stu_dict["age"] = age
                tel = input("请输入学生电话:")
                stu_dict["电话"] = tel
                id_num = "1902" + str(number).zfill(4)
                stu_dict["学号"] = id_num
                all_student.append(stu_dict)
                print("===添加成功!")
                with open('stu.json', 'w', encoding='utf-8') as f:
                    json1 = json.dumps(all_student)
                    f.write(json1)
                with open('number.json', 'w', encoding='utf-8') as f:
                    json2 = json.dumps(number)
                    f.write(json2)
                print("=====================================")
                print("1.继续""\n""2.返回 ")
                elect = int(input("请选择(1-2)"))
                if elect == 2:
                    break
        elif select == 2:
            while True:
                print("1. 查看所有学生")
                print("2. 按照姓名查找")
                print("3. 按照学号查找")
                print("4. 返回")
                print("=====================================")
                select2 = int(input("请选择(1-4):"))
                if select2 == 1:
                    for stu in all_student:
                        for index in stu:
                            print( index, ":", stu[index] ,end=" ")
                        print("")
                elif select2 == 2:
                    name_search = input("请输入要查找的学生姓名:")
                    for stu in all_student:
                        if stu["name"] == name_search:
                            print(stu)
                            break
                    else:
                        print("你查找的学生不存在")
                elif select2 == 3:
                    id_search = input("请输入要查找的学生学号:")
                    for stu in all_student:
                        if stu["学号"] == id_search:
                            print(stu)
                            break
                    else:
                        print("你查找的学生不存在")
                elif select2 == 4:
                    break

        elif select == 3:
            while True:
                print("1. 通过学号修改")
                print("2. 返回")
                print("=====================================")
                select4 = int(input("请选择(1-2):"))
                if select4 == 1:
                    id1 = input("请输入要修改信息的学生学号:")
                    change_stu = []
                    for stu in all_student:
                        if stu["学号"] == id1:
                            change_stu = stu
                            break
                    else:
                        print("你查找的学生不存在")
                        break
                    while True:
                        print("1. 修改姓名")
                        print("2. 修改年龄")
                        print("3. 修改手机号")
                        print("4. 返回")
                        print("=====================================")
                        select5 = int(input("请选择(1-4):"))
                        if select5 == 1:
                            change_stu["name"] = input("请输入修改后的姓名")
                            print("修改成功")
                        elif select5 == 2:
                            change_stu["age"] = int(input("请输入修改后的年龄"))
                            print("修改成功")
                        elif select5 == 3:
                            change_stu["tel"] = input("请输入修改后的学号")
                            print("修改成功")
                        elif select5 == 4:
                            break
                        with open('stu.json', 'w', encoding='utf-8') as f:
                            json1 = json.dumps(all_student)
                            f.write(json1)
                if select4 == 2:
                    break
        elif select == 4:
            while True:
                print("1. 通过学号修改")
                print("2. 返回")
                print("=====================================")
                select3 = int(input("请选择(1-2):"))
                if select3 == 1:
                    del_stu =  input("请输入要删除的学生学号:")
                    for index in range(len(all_student)):
                        if all_student[index]["学号"] == del_stu:
                            all_student.pop(index)
                            print("删除成功")
                            break
                    else:
                        print("你想要删除的学生不存在")

                elif select3 == 2:
                    break
                with open('stu.json', 'w', encoding='utf-8') as f:
                    json1 = json.dumps(all_student)
                    f.write(json1)
        elif select == 5:
            break
stu = founc1()


你可能感兴趣的:(day11_ task_学生管理系统Task)