首页 > 代码库 > 登录验证,错误三次锁定帐号

登录验证,错误三次锁定帐号

需求:用户的登录验证,登录错误三次锁定用户,用户名和密码保存在文件中

编译环境:python3

 

流程图如下:

 

技术分享

 

 

代码如下:

file = open(‘lock.txt‘).readlines()
name = input("username:\n>>").strip()
lock = []
for i in file:
    line = i.strip(‘\n‘)
    lock.append(line)
if name in lock:
    print(name, "已经被锁定")
else:
    i = 1
    while i <= 3:
        i += 1
        print(‘...........‘)
        username = name
        print(username)
        password = input(‘password:\n>>‘).strip()
        f = open(‘user.txt‘, encoding=‘utf-8‘).readlines()
        login = False
        if len(username) != 0 and len(password) != 0:  # 判断是否为合法输入
            for line in f:
                if username == line.split()[0] and password == line.split()[1]:  # 用户名和密码匹配正确
                    print(‘欢迎登录‘)
                    login = True
                    break
                else:
                    continue
        if login is False:
            print(u‘帐号或密码错误‘)
        if login is True:
                    break
    else:
        f = open(‘lock.txt‘, ‘a‘)
        f.write(name)
        f.write(‘\n‘)
        f.close()
        print(‘您的密码输入错误已达三次,帐号锁定,退出‘)

  

登录验证,错误三次锁定帐号