用户登录

1.用户名,密码可以输入3次

2.3次不对后锁定用户

#!_*_coding:utf-8_*_
#__author__:"tanxin"

#用于判断输入是否存在于文件中
def exist(name,filename):
    with open(filename, 'r') as f:
        name_list = ['']
        for i in f:
            i = i.split()[0]
            name_list.append(i)
        if name in name_list:
            result="true"
        else:
            result = "error"
        return result
#print(exist('tx1','account'))
for i in range(3):
    username = input("请输入用户名: ")
    re = exist(username,'lock')
    if re == 'error':  #函数返回值为error说明用户不在锁文件中,然后判断是否在用户文件中
        re1 = exist(username,'account')
        if re1 == 'error':
            print('%s is error account' % username)
            continue
        else:
            for c in range(3):  #输入3次密码
                password = input("请输入密码: ")
                with open("account", 'r') as p:
                    plist = ['']
                    for l in p:
                        flag = False
                        if username == l.split( )[0] and password == l.split( )[1]:
                            print("%s login success" % username)
                            flag = True
                            exit()
                        elif c == 2:
                            print('you can login 3 times')
                            print('%s is alerday locked' % username)
                            with open("lock", 'a') as p:
                                p.write(username+'\n')
                                exit()
                        elif flag == False:  #如果不设置这个标签,elif这里面无法判断
                            continue
                        else:
                            print('密码输入错误')
                            break

    else:
        print('%s is alerday locked' % username)
        break

 

posted @ 2018-04-12 14:54  cornerxin  阅读(174)  评论(0)    收藏  举报