美文网首页
day12_学生管理系统

day12_学生管理系统

作者: 逆流而上_2eb6 | 来源:发表于2018-10-15 17:54 被阅读0次
    def add_stu():
        """
        :return: 添加的学生的信息
        """
        global nextstu_id
        global stus_information
        name = input("请输入姓名:")
        age = input("请输入年龄:")
        phone = input("请输入电话:")
        password = input("请输入登陆密码")
        if password == "":
            password = name
        # 设置初始密码为name
        if name == "":
            print("增加失败")
            return
        # 名字为空则增加失败
        str_id = "python1807" + str(nextstu_id).rjust(3, "0")
        dict1 = {"name": name, "age": age, "phone": phone, \
                 "student_id": str_id, "password": password}
        print(dict1)
        print("学号为:" + str_id)
        nextstu_id += 1
        stus_information.append(dict1)
        with open("./stus_information.json", "w") as f3:
            json.dump([manage_name, manage_password, nextstu_id, stus_information], f3)
        print("增加成功")
        return dict1
    
    
    def find_stu(student_name: str):
        """
        :param student_name: 名字
        :return: 查找的学生
        """
        global stus_information
        num = 0
        list2 = []
        for n in stus_information:
            if n["name"] == student_name:
                list2.append(n)
                # 得到查找的学生
                print(num, n)
                num += 1
        if num == 0:
            print("没有该学生")
        return list2
    
    
    def del_stu(student: dict):
        """
        :param student:要删除的学生
        """
        global stus_information
        for s in stus_information:
            if s == student:
                stus_information.remove(student)
                with open("./stus_information.json", "w") as f3:
                    json.dump([manage_name, manage_password, nextstu_id, stus_information], f3)
                print("删除成功")
                break
    
    
    def change_stu(student: dict):
        """
        :param student: 要修改的学生
        """
        global stus_information
        key1 = input("请输入要修改的信息")
        if key1 not in student.keys():
            print("输入信息有误,修改失败!!!")
        elif key1 == "student_id":
            print("不能修改学号!!!")
        else:
            value1 = input("请输入修改后的值")
            for s in range(len(stus_information)):
                if stus_information[s] == student:
                    stus_information[s][key1] = value1
                    print(stus_information[s])
                    print("修改成功")
                    with open("./stus_information.json", "w") as f3:
                        json.dump([manage_name, manage_password, nextstu_id, stus_information], f3)
    
    
    def manage_land():
        if manage_name == input("请输入管理员名称") \
                and manage_password == input("请输入管理员密码"):
            return True
        print("输入出错!!!")
        return False
    
    
    def print_operation(s: str):
        """
        :param s:操作及其编号
        """
        num = num_interface - 5 - (len(s) - 1) + 10
        print("║" + " " * 5 + s.ljust(num) + " ║")
        # 不想解释,一个汉字占两空格,
        # bug:格式只能是:单个数字.汉字,有英文界面会乱
    
    
    num_interface = 20
    # 界面长度
    start_interface = "╔" + "欢迎来到学生管理系统".center(num_interface, "-") + "╗"
    end_interface = "╚" + "请输入执行的操作编号".center(num_interface, "-") + "╝"
    
    
    # 登陆界面
    def land_interface():
        """
        :return: 操作编号
        """
        print(start_interface)
        print_operation("1.学生登陆")
        # 只能修改查看自己的信息
        print_operation("2.管理员登陆")
        # 可以有所有权限
        print_operation("3.注册")
        # 加入一个新学生
        print_operation("4.退出系统")
        print(end_interface)
        operation_num = input()
        return operation_num
    
    
    # 返回界面
    def bank_interface():
        print(start_interface)
        print_operation("1.再次操作")
        print_operation("2.返回上一层")
        print(end_interface)
        return input()
    
    
    # 修改界面
    def chang_intetface():
        print(start_interface)
        print_operation("1.修改信息")
        print_operation("2.返回上一层")
        print(end_interface)
        return input()
    
    
    # 管理员界面
    def manage_interface():
        print(start_interface)
        print_operation("1.增加学生")
        print_operation("2.查找学生")
        print_operation("3.删除学生")
        print_operation("4.显示所有学生")
        print_operation("5.修改学生信息")
        print_operation("6.修改管理员信息")
        print_operation("7.返回上一层")
        print(end_interface)
        return input()
    
    
    # 学生管理系统
    import json
    try:
        with open("./stus_information.json", "r") as f1:
            information = json.load(f1)
            stus_information = information[3]
            # 学生信息
            nextstu_id = information[2]
            # 下一个学生的学号
            manage_password = information[1]
            manage_name = information[0]
    except:
        print("欢迎使用学生管理系统,首次登陆!!")
        manage_name = input("请设置管理员名称:")
        manage_password = input("请设置登陆密码")
        nextstu_id = 1
        stus_information = []
        with open("./stus_information.json", "w") as f2:
            json.dump([manage_name, manage_password, nextstu_id, stus_information], f2)
    while 1:
        operation_num = land_interface()
        # 登陆界面
        while 1:
            # 学生登陆
            if operation_num == "1":
                my_id = input("请输入学号:")
                my_password = input("请输入密码:")
                for s in stus_information:
                    if s["student_id"] == my_id and s["password"] == my_password:
                        # 验证密码和学号
                        print(s)
                        break
                # 密码错误时
                else:
                    print("密码或学号错误!!!")
                    operation_num1 = bank_interface()
                    if operation_num1 != "1":
                        # 除1以外任意输入返回上一层
                        break
                    else:
                        continue
                # 密码正确时
                operation_num1 = chang_intetface()
                # 修改学生信息
                if operation_num1 == "1":
                    while 1:
                        # 循环可以多次修改信息
                        change_stu(s)
                        operation_num11 = bank_interface()
                        if operation_num11 != "1":
                            break
                break
                # 回到主界面
            # 管理员登陆
            elif operation_num == "2":
                if not manage_land():
                    input("任意键返回")
                    break
                while 1:
                    operation_num2 = manage_interface()
                    if operation_num2 == "1":
                        add_stu()
                        input("任意键退出")
                    elif operation_num2 == "2":
                        name2 = input("请输入要查找的学生的姓名:")
                        find_stu(name2)
                        input("任意键退出")
                    elif operation_num2 == "3":
                        name3 = input("请输入要删除的学生的姓名:")
                        students = find_stu(name3)
                        if len(students) == 0:
                            input("任意键退出")
                            continue
                        else:
                            print("请确认删除的学生,并输入对应的编号")
                            num3 = input()
                            if num3.isnumeric() and int(num3) in range(0, len(students)):
                                del_stu(students[int(num3)])
                        input("任意键退出")
                    elif operation_num2 == "4":
                        if len(stus_information) == 0:
                            print("还没有学生")
                        for s in stus_information:
                            print(s)
                        input("任意键退出")
                    elif operation_num2 == "5":
                        name5 = input("请入要修改学生的名字")
                        students = find_stu(name5)
                        if len(students) == 0:
                            input("任意键退出")
                            continue
                        num5 = input("请确定要修改的学生并输入编号:")
                        if num5.isnumeric() and int(num5) in range(0, len(students)):
                            num5 = int(num5)
                            while 1:
                                change_stu(students[num5])
                                operation_num3 = bank_interface()
                                if operation_num3 != "1":
                                    break
                        else:
                            print("没有该学生,输入编号出错")
                    elif operation_num2 == "6":
                        if not manage_land():
                            input("任意键返回")
                            continue
                        manage_name = input("请输入更改后的管理员名称:")
                        manage_password = input("请输入更改后的登陆密码")
                    else:
                        break
                break
            # 注册
            elif operation_num == "3":
                add_stu()
                input("任意键退出")
                break
            # 退出系统
            else:
                exit()
    

    相关文章

      网友评论

          本文标题:day12_学生管理系统

          本文链接:https://www.haomeiwen.com/subject/kdqqzftx.html