python3 验证用户名密码

时间:2023-04-20 17:00:25

输入用户名,密码,匹配通过,不匹配报错

import getpass

user = input('input username: ')
pwd = getpass.getpass('input password: ') if user == 'xp' and pwd == 'xp0001':
print("welcome %s" % user)
else:
print("Error username or password")

  

增加计数,大于3次报错

import getpass
user_info = {} a = {'xp':{'pwd':'xp0001','block':0}} count = a.get('xp')['block']
print(count)
while count < 3:
user = input('input username: ')
pwd = getpass.getpass('input password: ') if user == 'xp' and pwd == 'xp0001':
print("welcome %s" % user)
break
else:
print("Error username or password,retry") count += 1
else:
print("%s is locked" % user)
a['xp']['block'] = 3

  

可是这样再执行的时候,block值还是返回0,增加个文件吧