学生管理系统β版

学生管理系统
输入用户名和密码,现在users.json文件查找用户名密码是否正确,正确登录,不正确就返回错误信息,并且继续输入用户密码
登录成功后,根据相应的用户名找到对应的json文件,取出里面数据保存转换为python数据,保存在程序中,按照提示进行选择
如果想要删除或者修改,先进入查询系统,选择查询方式,找到相应的学生之后才能删除或者修改

import json


def find_all(student_list):
    """
    查询所有学生的成绩
    :param student_list: 学生列表
    :return:
    """
    print("以下是全部学生成绩:")
    for x in range(len(student_list)):
        for key in student_list[x]:
            print('{}:{}'.format(key, student_list[x][key]), end=" ")  # key + ":" + student_list[x][key]
        else:
            print("\n")
    flag1 = '1'
    while not flag1.isdigit() or eval(flag1):
        flag1 = input("输入数字0返回上一级")


def update(student_list, x):
    """
    修改学生信息
    :param x: 索引值
    :param student_list: 学生列表
    :return:
    """
    student = student_list[x]
    print("--------------------------------------------------")
    print("欢迎进入学生信息修改系统")
    print("需要修改的学生信息为:{}".format(student))
    print("1.Name 2.EScore 3.SScore 4.AScore 5.MScore 6.Age")
    flag3 = input("请输入需要修改的项目(输入数字0返回上一级):")
    while flag3 != '0':
        if flag3 == '1':
            print("原信息为:", student['Name'])
            student['Name'] = input('请输入新的信息:')
        elif flag3 == '2':
            print("原信息为:", student['EScore'])
            student['EScore'] = eval(input('请输入新的信息:'))
        elif flag3 == '3':
            print("原信息为:", student['SScore'])
            student['SScore'] = eval(input('请输入新的信息:'))
        elif flag3 == '4':
            print("原信息为:", student['AScore'])
            student['AScore'] = eval(input('请输入新的信息:'))
        elif flag3 == '5':
            print("原信息为:", student['MScore'])
            student['MScore'] = eval(input('请输入新的信息:'))
        elif flag3 == '6':
            print("原信息为:", student['Age'])
            student['Age'] = eval(input('请输入新的信息:'))
        print("更新成功")
        print("更新后的信息为:", student)
        write_file(name, student_list)
        flag3 = input("请输入需要修改的项目(输入数字0返回上一级):")
    return student_list


def _detele(student_list, student):
    print("您需要删除信息为:", student)
    student_list.remove(student)
    print("删除成功")
    write_file(name, student_list)
    return student_list


def find_by_name(student_list):
    student_name = input("请输入姓名:")
    number = 1
    i = 1
    new_student_list = []
    while i:
        for x in range(len(student_list)):
            if student_list[x].get("Name") == student_name:
                print(number, student_list[x])
                new_student_list.append(x)
                number += 1
                i = 0
        if i:
            print("该学生不存在!")
            student_name = input("请重新输入姓名:")
    return new_student_list


def find_by_sno(student_list):
    sno = input('请输入学号')
    new_student_list = []
    i = 1
    while i:
        for x in range(len(student_list)):
            if student_list[x].get("Sno") == sno:
                print('1', student_list[x])
                new_student_list.append(x)
                i = 0
        if i:
            print("该学生不存在!")
            sno = input("请重新输入学号:")
    return new_student_list


def find(student_list):
    """
    学生查询并且修改删除
    :param student_list:
    :return:
    """
    print("欢迎进入学生信息查询修改系统")
    flag4 = '1'
    while not flag4.isdigit() or eval(flag4):
        choose_number = '0'
        while not choose_number.isdigit() or not 0 < int(choose_number) <= 2:
            choose_number = input('1.输入姓名查找\n'
                                  '2.输入学号查找\n'
                                  '请输入数字:')
        if eval(choose_number) == 1:
            new_student_list = find_by_name(student_list)
            choose = eval(input('请输入要操作的学生的序号'))
        elif eval(choose_number) == 2:
            new_student_list = find_by_sno(student_list)
            choose = 1
        while choose <= 0 or choose > len(new_student_list):
            choose = eval(input('请输入要操作的学生的序号'))
        x = new_student_list[choose - 1]
        flag2 = '1'
        while flag2:
            flag2 = eval(input("1.修改该学生信息请输入数字1\n"
                               "2.删除该学生信息请输入数字2\n"
                               "输入数字0返回上一级\n"
                               "请输入数字:"))
            if int(flag2) == 1:
                student_list = update(student_list, x)
            elif int(flag2) == 2:
                student_list = _detele(student_list, student_list[x])
                break
        flag4 = input('输入数字0返回上一级,输入数字1查询其他')
    return student_list


def get_number(student):
    number = student['Sno'][-1:-4:-1]
    number = number[::-1]
    while number.startswith('0'):
        number = number[1:]
    return number


def list_sort(student_list):
    if len(student_list) == 0:
        return "1"
    for x in range(len(student_list) - 1):
        for y in range(len(student_list) - x - 1):
            if get_number(student_list[y]) > get_number(student_list[y + 1]):
                student_list[y], student_list[y + 1] = student_list[y + 1], student_list[y]
    return get_number(student_list[-1])


def gen_new_sno(class_name, last_number):
    while True:
        new_sno = class_name + str(last_number + 1).rjust(3, '0')
        yield new_sno


def add(student_list, last_number):
    """
    添加学生信息
    :param last_number:
    :param student_list: 学生列表
    :return:
    """
    print("进入学生信息添加系统")
    student = {}
    student.setdefault("Name", input("请输入学生姓名:"))
    class_name = input("请输入学生类型")
    new_sno = gen_new_sno(class_name, last_number)
    student.setdefault("Sno", next(new_sno))
    student.setdefault("EScore", eval(input("请输入学生英语成绩:")))
    student.setdefault("SScore", eval(input("请输入学生体育成绩:")))
    student.setdefault("AScore", eval(input("请输入学生艺术成绩:")))
    student.setdefault("MScore", eval(input("请输入学生数学成绩:")))
    student.setdefault("Age", eval(input("请输入学生年龄:")))
    student_list.append(student)
    print("添加完成")
    write_file(name, student_list)
    return student_list


def read_file(f_name):
    file_name = './' + f_name + '.json'
    with open(file_name, 'r', encoding='utf-8') as f:
        new_list = json.load(f)
    return new_list


def write_file(f_name, student_list):
    file_name = './' + f_name + '.json'
    with open(file_name, 'w', encoding='utf-8') as f:
        json.dump(student_list, f, ensure_ascii=False, indent=4)


def verification(uer_name, u_keywords):
    users = read_file('users')
    for x in range(len(users)):
        if users[x]['Name'] == uer_name:
            if users[x]['keywords'] == u_keywords:
                loading_message = "登录成功"
                return False, loading_message
            else:
                loading_message = '密码错误'
                return True, loading_message
    else:
        loading_message = '用户名错误'
        return True, loading_message


def register():
    new_users = {}
    register_name = input('输入您的用户名:')
    register_keywords = input('输入您的密码:')
    users_list = read_file('users')
    for x in range(len(users_list)):
        if users_list[x]["Name"] == register_name:
            print("用户名已经存在,请重新输入")
            register()
            break
    else:
        new_users['Name'] = register_name
        new_users['keywords'] = register_keywords
        users_list.append(new_users)
        write_file('users', users_list)
        register_student_list = []
        new_file_name = './' + register_name + '.json'
        write_file(new_file_name, register_student_list)
        print('注册成功')


def longing():
    longing_name = input('输入您的用户名:')
    keywords = input('输入您的密码:')
    value, message = verification(longing_name, keywords)
    while value:
        print(message)
        longing_name = input('输入您的用户名:')
        keywords = input('输入您的密码:')
        value, message = verification(longing_name, keywords)
    print(longing_name)
    return longing_name


def longing_or_register():
    print("************************************************")
    print("*                 请选择操作                   *")
    print("*                 1.登录                       *")
    print("*                 2.注册                       *")
    print("************************************************")
    i = input("请输入数字:")
    if i == '1':
        return longing()
    elif i == '2':
        register()
        longing_or_register()


if __name__ == '__main__':
    name = longing_or_register()
    StudentList = read_file(name)
    last_number = int(list_sort(StudentList))
    flag = '1'
    while (not flag.isdigit()) or (eval(flag)):
        print("************************************************")
        print("*          欢迎{}进入学生管理系统               *".format(name))
        flag = input("*          1.输入数字1查询班级所有学生          *\n"
                     "*          2.输入数字2根据姓名查找或者修改学生   *\n"
                     "*          3.输入数字3添加学生                  *\n"
                     "*          4.输入数字0退出系统                  *\n"
                     "************************************************\n"
                     "请输入数字:"
                     )
        if flag.isdigit():
            if int(flag) == 1:
                find_all(StudentList)
            elif int(flag) == 2:
                find(StudentList)
            elif int(flag) == 3:
                StudentList = add(StudentList, last_number)

你可能感兴趣的:(学生管理系统β版)