需求:用户名输入3次不对后即被锁定
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:hx counter = 0 #添加计数器 #创建黑名单文档,第一次创建文档时使用,文档被创建后即可隐去。 #f = open('black_user.txt','w') #f.close() file = open('black_user.txt','r') lock_file = file.read() file.close() username = 'hx' password = 'hx' user_name = input('please input your name:') for i in range(1): if user_name == lock_file: print("你的用户已被锁定,请联系管理员") exit() else: continue for i in range(4): if counter<3: user_pass = input('please input your password') if password == user_pass: print("welcome to login.....") break else: print("please login again...") counter = counter + 1 else: print("你输入超过3次,你输入的用户将被锁定,请联系管理员") file = open('black_user.txt','w') file.write('%s'%user_name) file.close()