2018-10-16学生管理系统利用json数据本地化添加登录注册

import json

def tishi():
    print('*********1.返回主页面**********')
    print('*********0.退出系统************')
    while True:
        num2 = int(input("请选择操作:"))
        if num2 == 1:
            return
        if num2 == 0:
            exit()
        else:
            print('********请正确操作!********')


def add_stu():
    addname = input("请输入添加的学生姓名:")
    addstu_id = input("请输入添加学生的学号:")
    addage = input('请输入添加的学生年龄:')

    addscore1 = int(input('请输入添加的学生英语成绩:'))
    addscore2 = int(input('请输入添加的学生体育成绩:'))
    addscore3 = int(input('请输入添加的学生美术成绩:'))
    addscore4 = int(input('请输入添加的学生数学成绩:'))

    students.append({'name': addname, 'stu_id': addstu_id, 'age': addage,
                     'scores': {'英语': addscore1, '体育': addscore2, '美术': addscore3, '数学': addscore4}})
    with open('./student.json', 'w') as f2:
        json.dump(students, f2)
    print("添加成功!")
    tishi()


def find_stu():
    findname = str(input("请输入要查询的学生姓名:"))
    for student in students:
        if student['name'] == findname:
            print("姓名:", student['name'])
            print("学号:", student['stu_id'])
            print("年龄:", student['age'])
            print("英语成绩:", student['scores']['英语'])
            print("体育成绩:", student['scores']['体育'])
            print("美术成绩:", student['scores']['美术'])
            print("数学成绩:", student['scores']['数学'])
            break
    tishi()


def del_stu():
    delname = str(input("请输入要删除的学生姓名:"))
    for student in students:
        if student['name'] == delname:
            students.remove(student)
            print('删除成功!')
            break
    with open('./student.json', 'w') as f2:
        json.dump(students, f2)
    tishi()


def show_stu():
    print("*姓名****学号****年龄***英语***体育***美术***数学*")
    for student in students:
        print(student['name'], '', student['stu_id'], '', student['age'], '   ', student['scores']['英语'], '   ',
              student['scores']['体育'], '   ', student['scores']['美术'], '   ', student['scores']['数学'])
    tishi()


def score_stu():
    stu_name = str(input("请输入要查询分数的学生姓名:"))
    for student in students:
        if student['name'] == stu_name:
            averge_score = float((student['scores']['英语'] + student['scores']['体育'] + student['scores']['美术'] +
                                  student['scores']['数学'])) / 4
            break
    print('他的平均分为:', averge_score)
    tishi()


def user_login():
    with open('./user.json', 'r', encoding='utf-8') as f1:
        all_user = json.load(f1)
    user_name = str(input("请输入姓名:"))
    for user in all_user:
        if user["name"] == user_name:
            count = 0
            while True:
                user_password = str(input("请输入密码:"))
                if user["password"] == user_password:
                    print("登陆成功!")
                    return
                else:
                    print("密码错误!")
                    count += 1
                    if count >= 3:
                        print("密码输错3次,自动退出系统....")
                        exit()

    else:
        print("对不起没有这个用户,请注册!")
        add_user()
        return


def add_user():
    # 保存所有用户信息
    with open('./user.json', 'r', encoding="utf-8") as f1:
        all_user = json.load(f1)

    user_name = input("请输入要注册的姓名:")
    user_password = input("请输入密码:")
 #   user_password2 = str(input("请再次输入密码:"))

    user = {'name': user_name, 'password': user_password}
    all_user.append(user)

    with open('./user.json', 'w') as f2:
        json.dump(all_user, f2)
    print(all_user)


while True:
    print("**********************************")
    print("1.用户登录")
    print("2.请注册新用户")
    print("0.退出系统")
    print("**********************************")

    ans = int(input("请选择操作:"))
    if ans == 0:
        exit()
    elif ans == 1:
        user_login()
        break
    elif ans == 2:
        add_user()
        break
    else:
        print("请正确操作")


while True:
    print('****************************欢迎来到千锋学生管理系统*************************')
    print('1 添加学生\n')
    print('2 查询学生\n')
    print('3 删除学生\n')
    print('4 查看班级所有学生\n')
    print('5 查询学生平均成绩\n')
    print('0 退出系统\n')
    print('*****************************************************************************')

    num = int(input('请选择你的操作:'))

    with open('./student.json', 'r', encoding='utf-8') as f1:
        students = json.load(f1)

    # students = [{'name': '小明', 'stu_id': 5120152566, 'age': 18, 'scores': {'英语': 96, '体育': 86, '美术': 84, '数学': 98}}]
    if num == 0:
        exit()

    if num == 1:
        add_stu()
     #   print(students)

    if num == 2:
        find_stu()

    if num == 3:
        del_stu()
    #    print(students)

    if num == 4:
        show_stu()

    if num == 5:
        score_stu()

你可能感兴趣的:(2018-10-16学生管理系统利用json数据本地化添加登录注册)