Python练习题
模拟用户登录,检测用户输入的用户名和密码
- 准备一个字典,保存用户名及对应的密码
- 若用户名不存在,则提示’User Not Allowed’
- 若用户名存在,但密码不正确,则提示’Authentication failure’
- 若用户名和密码都正确,则登录成功,输出:xxx@localhost:~$
- 当用户登录失败后可重试3次,每次重试需间隔5s钟,累计3次失败后,退出登录程序
dict = {"daisy":'123456789',"thosaka":'1122334455',"eric" : '2255889966',"misakamikoto":'667788' }
import time
def login():
c = 3
while c > 0:
username = input("please enter your username:")
password = input("please enter your password:")
if username in dict:
if dict[username] == password:
print(f"登录成功,欢迎 {username}@localhost:~$")
break
else:
print("Authentication failure")
else:
print("user not found")
c -= 1
if c > 0:
print(f'you have {c} chance')
time.sleep(5)
else:
print('you have try a lot,quit')
login()