1 #!/usr/bin/env python 2 import sys 3 import getpass 4 afile = ‘afile‘ 5 bfile = ‘bfile‘ 6 circulation_num=0 #循环次数初始基数 7 def deny_account(username): 8 print("This account already locked!") 9 with open(bfile, ‘a+‘) as bf: #此处最好为a+模式,本人之前使用a模式错误账号无法写入 10 bf.write(username + ‘\n‘) 11 #循环开始 12 while circulation_num<3: 13 username = input("\033[32;1mPlease input your username:\033[0m") 14 flag = False 15 with open(afile, ‘r‘) as af: 16 for line in af.readlines(): 17 user,pwd=line.strip().split() 18 if username==user: 19 password=getpass._raw_input(‘Please input password:‘).strip() 20 if username==user and password ==pwd: 21 print(‘success!‘) 22 flag = True 23 break 24 else: 25 print(‘password error,please try again!‘) 26 else: 27 if circulation_num < 3: 28 print(‘User doesn`t exist!‘) 29 circulation_num += 1 30 break 31 if flag == True: 32 print(‘Welcome %s come in TG!‘ % username) 33 break 34 else: 35 deny_account(username)
原文:http://www.cnblogs.com/carl-angela/p/5399520.html