day12-作业

登陆和注册功能

代码如下

import json
user = []
with open('user.json', 'r') as fp:
    user = json.load(fp)


def region():
    while 1:
        user1 = input('请输入用户名:')
        password1 = input('请输入密码:')
        us = False
        true = False
        if user == []:
            print('此系统还没有人注册,请注册')
            print('是否注册:1.是 2.否')
            n = int(input('请选择:'))
            if n == 1:
                register()
            else:
                break
        for user2 in user:
            if user2['user'] == user1:
                us = True
                if user2['user'] == user1 and user2['password'] == password1:
                    true = True
        if us == False:
            print('用户名不存在!')
            break
        elif true:
            print('登陆成功!')
            break
        else:
            print('用户名或密码错误,请重新输入,是否重新输入:1.是 2.否')
            n = int(input('请选择:'))
            if n == 1:
                continue
            else:
                break


def register():
    while 1:
        user3 = input('请输入用户名:')
        password1 = input('请输入密码:')
        password2 = input('请确认密码:')
        name = False
        if password1 != password2:
            print('两次密码不一致,请重新输入!')
            continue
        for user2 in user:
            if user2['user'] == user3:
               name = True
        if name:
            print('用户名已存在,请重新输入!')
            continue
        user_ = {'user': user3, 'password': password1}
        user.append(user_)
        break
    with open('user.json', 'w') as fp:
        json.dump(user, fp)
    print('注册成功!')

while 1:
    print('==============================')
    print('|    欢迎来到学生管理系统    |')
    print('==============================')
    print('|        1.用户登陆          |')
    print('|        2.用户注册          |')
    print('==============================')
    n = int(input('请选择:'))
    if n == 1:
        region()
    elif n == 2:
        register()
    else:
        print('您已退出系统!')
        break

运行结果可自行检测

你可能感兴趣的:(day12-作业)