Day11作业

login_register.py

def login_register():
    print('登录注册:')
    print('======================================================')
    print('1.登录\n2.注册')
    choice = input('请选择(1-2):')

    if choice == '1':
        import logon
    else:
        import login

print('======================================================')

login_register()

login.py

import json

filename = 'username_password.json'

    
def logon():
    print('======================================================')
    print('注册')

    with open(filename) as f_obj:         #打开存储所有用户的json文件并读取
        all_user = json.load(f_obj)

    username = input('输入账号:')
    password = input('输入密码:')
    user = {username: int(password)}

    all_user.append(user)          #将新注册的用户账号及密码添加在json文件存储所有用户的列表中

    with open(filename,'w') as f_obj:
        json.dump(all_user,f_obj)     #再将改的后的所有用户信息添加入json文件

    print('注册成功')
    import login_register

logon()

logon.py

import json

filename = 'username_password.json'

def login():
    print('======================================================')
    print('登录:')
    username = input('请输入账号:')
    password = input('请输入密码:')

    user = {username: int(password)}  # 用户名和密码存入字典

    with open(filename) as f_obj:
        all_user = json.load(f_obj)

        if user in all_user:  # 判断用户是否存在
            print('登陆成功')
        else:
            print('登录失败\n请注册')
            import login

    print('======================================================')


login()

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