【python练习】登陆接口

功能输入用户名密码、认证成功后显示欢迎信息、输错三次后锁定,有保存功能

'''
功能:
用户输入用户名密码
认证成功后显示欢迎信息
输错三次后退出程序
支持多个用户登录
用户3次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态
'''

'''
测试账户:
liubei lb123
guanyu gy123
zhangfei 锁定状态
'''

import os
acc=''
auth_list=[]#用于存放每次输入的用户名

print('-'*6+' Login '+'-'*6)
while auth_list.count(acc)<3: #当相同的用户名登陆失败3次,跳出循环
    acc = input('Account:').strip()#用户名、密码输入
    pwd = input('Password:').strip()

    path='data\\%s'%acc    #用户信息存放路径
    if os.path.isfile(path):#用户名是否存在
        with open(path) as f:#文件是否存在
            data=f.read()
            if data.split(' ')[0]=='True': #用户是否被锁定
                if data.split(' ')[2]==pwd:
                    print('Login success !')
                    exit()
                else:
                    auth_list.append(acc)
                    print('The password is error,you have %s times'%(3-auth_list.count(acc)))
            else:
                print('You have tried 3 times,your account has locked.')
                exit()
    else:
        auth_list.append(acc)
        print('The account is not exist...you have %s times'%(3-auth_list.count(acc)))

if auth_list.count(acc)==3:#已存在的用户登陆失败三次,锁定,写入文件
    if os.path.isfile(path):
        with open(path,'w')as f:
            data='False'
            f.write(data)

文件:../data/下,以用户名命名文件

guanyu
liubei

 

 

 

posted @ 2018-04-17 22:03  q1ang  阅读(206)  评论(0编辑  收藏  举报