python基础 day17 模拟博客园登录

import os
login_status = {'user': '', 'status': False}


def get_userdic():
    user_dic = dict()
    with open('user.txt', 'r', encoding='utf-8') as f1:
        for line in f1:
            user_dic.update({line.strip().split('|')[0]: line.strip().split('|')[1]})
    return user_dic


def register():
    user_dic = get_userdic()
    count = 0
    while count < 3:
        new_name = input('请输入新用户名>>>').strip()
        new_password = input('请输入新密码>>>').strip()
        if new_name not in user_dic:
            if new_name.isalnum():
                if 6 < len(new_password) < 14:
                    with open('user.txt', 'a', encoding='utf-8') as f1:
                        f1.write(f'{new_name}|{new_password}\n')
                    print('添加成功')
                    break
                else:
                    print('密码长度要在6~14个字符之间')
                    count += 1
            else:
                print('用户名只能含有字母或者数字不能含有特殊字符')
                count += 1
        else:
            print('用户名已存在')
            count += 1


def remove_user():
    while 1:
        remover = input('请输入要注销的账号(q退出):').strip()
        user_dic = get_userdic()
        if remover in user_dic:
            pw = input('请输入密码:').strip()
            if pw == user_dic[remover]:
                user_dic.pop(remover)
                with open('new_user.txt', 'w') as f1:
                    for u, p in user_dic.items():
                        f1.write(f'{u}|{p}')
                os.remove('user.txt')
                os.rename('new_user.txt', 'user.txt')
        elif remover.upper() == 'Q':
            break
        else:
            print('账号不存在')


def login():
    user_dic = get_userdic()
    count = 0
    while count < 3:
        username = input('请输入用户名(q退出):').strip()
        if username.upper() == "Q":
            break
        password = input('请输入密码:').strip()
        if username in user_dic and user_dic[username] == password:
            login_status['use'] = username
            login_status['status'] = True
            break
        else:
            print('账号密码错误,重新输入')
            count += 1


def auth(f):
    def inner(*args, **kwargs):
        if login_status['status']:
            ret = f(*args, **kwargs)
            return ret
        else:
            login()
            ret = f(*args, **kwargs)
            return ret
    return inner


@auth
def article():
    while 1:
        print('欢迎进入文章页面')
        enter = input('>>>(q返回)')
        if enter.upper() == 'Q':
            break


@auth
def photo():
    while 1:
        print('欢迎进入照片页面')
        enter = input('>>>(q返回)')
        if enter.upper() == 'Q':
            break


@auth
def file():
    while 1:
        print('欢迎进入文件页面')
        enter = input('>>>(q返回)')
        if enter.upper() == 'Q':
            break


dic_home = {
    '1 请登录': login,
    '2 请注册': register,
    '3 进入文章页面': article,
    '4 进入照片页面': photo,
    '5 进入文件页面': file,
    '6 注销账号': remove_user,
    '7 退出程序': quit,
}
while 1:
    for key in dic_home:
        print(key)
    user_choose = (input('>>>'))
    for key in dic_home:
        if user_choose == key[0]:
            dic_home.get(key)()

猜你喜欢

转载自www.cnblogs.com/west-yang/p/12694011.html