模拟登陆:
1. 用户输入帐号密码进行登陆
2. 用户信息保存在文件内
3. 用户密码输入错误三次后锁定用户
count=0#循环定数
username='lisi'#给定用户
userpassword='abc'#给定密码
lock=[]#给定帐号锁定列表
f=open('black_mingdan.txt','r')#打开黑名单文件
file_list=f.readlines()#按每行读取
f.close()#关闭文件,已免造成资源浪费
name=input('pls input your name:')#输入用户
for i in file_list:#循环判断黑名单文本里是否有输入的帐号
line=i.strip()
lock.append(line)
if name in lock:#如果输入的帐号是文本中存在的
print('username lock')#则提示帐号锁定
else:
# if name==username:
for i in range(3):#否则,循环输入3次
if count<3:
pwd=input('pls input your pwd:')#输入密码
if pwd==userpassword and name==username:#如果帐号和密码同时相等
print('welcome')#则进入
break#跳出整个循环
else:
print('帐号或密码不对')
count += 1#每次加1
# continue
else:
print("你的帐号已输3次,bye!!")#如输入3次都是错语,则锁定
f=open('black_mingdan.txt','w')
user=['%s' %username]
f.writelines(user)
f.close()
# else:
# print('请重新输入')
第二种:
lock=[] count=0 _username='alex' _pwd=123 while True: username=input("请输入用户名:") pwd=int(input("请输入密码:")) if username in lock: print("sorry,you username lock,bye bye...") break if username==_username and pwd==_pwd: print("welcome to {0}".format(username)) print("welcome login") break else: print("username and pwd error") count+=1 if count==3: lock.append(username) print("username lock")