Python基础案例密码测试

允许输入三次密码,三次后清空,账户锁定

import os
import time
import json
#初始为0
count = 0
exit_flag = False
while count < 3:
    user = input('请输入用户名')
    f = user.strip() + '.json'
    #借助判断文件是否存在,判断用户是否存在
    if os.path.exists(f):
    #打开文件,格式utf-8
        fp = open(f, 'r+', encoding='utf-8')
        #使用json加载文件内容
        j_user = json.load(fp)
        #判断是否锁定
        if j_user['status'] == 1:
            print('账户已经锁定')
            break
        else:
        #判断是否过期
            expire_dt = j_user['expire_date']
            current_dt = time.time()
            expire_dt = time.mktime(time.strptime(expire_dt,'%Y-%m-%d'))
            if current_dt > expire_dt:
                print('用户已经过期')
                break
            else:
                while count < 3:
                    pwd = input('请输入密码:')
                    if pwd.strip() == j_user['password']:
                        print('登录成功')
                        exit_flag = True
                        break
                    else:
                        if count == 2:
                            print('输入密码超过3次,账户锁定')
                            j_user['status'] = 1
                            fp.seek(0)
                            fp.truncate()
                            json.dump(j_user, fp)
                        else:
                            print('密码错误重新输入')
                        count += 1
    else:
        print('用户不存在')
        count += 1
    #判断是否锁定
    if exit_flag:
        break

猜你喜欢

转载自blog.csdn.net/qq_31235811/article/details/88766159