python3 实现用户三次输入错误记录黑名单并对该用户登录锁定.
1 # Author:student 2 import getpass #输入密码时密文,这个输入不能通过pycharm执行 3 import re 4 _username = 'justtest' 5 _password = 'Dell@123' 6 7 #print(username,password) 8 count =0 9 username = input("username:") 10 lockfile = [] 11 f = open('lockuser.txt','r',encoding='gbk') 12 lockfile = f.readline() 13 f.close() 14 list = [] 15 for line in open("lockuser.txt"): 16 line1 = line.strip('\n') 17 list.append(line1) 18 if username in list: 19 print ("is locked") 20 else: 21 while count <=3: 22 if count == 3: 23 print("you try too many times for the password and your account will locked") 24 f = open('lockuser.txt', 'a', encoding='utf-8') 25 f.write(username+"\n") 26 f.close() 27 break 28 else: 29 #password = getpass.getpass("password") 30 password = input("password:") 31 if _username == username and _password == password: 32 print("Welcome user {name} login....".format(name=username)) 33 break 34 else: 35 print("Invalid username or password!") 36 count +=1