输入账号密码完成验证,验证通过后输出"登录成功"
dic={"name":'admin', "password":'123'}
name = input(">>>user:")
password = input(">>>password:")
if name in dic["name"] and password in dic["password"]:
print("登录成功")
else:
print("登录成功")
可以登录不同的用户
dics = {
"admin": {"password": '123'},
"egon": {"password": '123'}
}
name = input(">>>user:")
password = input(">>>password:")
if name in dics and password == dics[name]["password"]:
print("登录成功")
else:
print("登录失败")
同一账号输错三次锁定(附加功能,在程序一直运行的情况下,一旦锁定,则锁定5分钟后自动解锁)
import time
dics = {
"admin": {"password": '123'},
"egon": {"password": '123'}
}
count = 0
while True:
name = input(">>>user:")
password = input(">>>password:")
if count < 3 and name in dics and password == dics[name]["password"]:
print("登录成功")
break
else:
print("登录失败")
if count < 3:
count += 1
continue
else:
print("该账号已被锁定,五分钟后重试")
time.sleep(300)
count = 0
continue
扩展需求:在3的基础上,完成用户一旦锁定,无论程序是否关闭,都锁定5分钟
import time, os
dics = {
"admin": '123',
"egon": '123'
}
count = 1
while True:
if os.path.exists('lock.txt'):
with open('lock.txt', 'r', encoding='utf-8') as f:
ti = f.read()
if ti == '300':
print("已被锁定无法输入,五分钟后解锁")
time.sleep(int(ti))
with open('lock.txt', 'w', encoding='utf-8') as f:
f.write("0")
print("解锁成功!请输入")
name = input(">>>user:")
password = input(">>>password:")
if name in dics:
if count < 3 and password == dics.get(name):
print("登录成功")
break
else:
print("密码错误,登录失败")
if count < 3:
count += 1
else:
with open('lock.txt', 'w', encoding='utf-8') as f:
f.write("300")
print("已被锁定五分钟")
else:
print("账号不存在")