"""
写一个函数完成三次登陆功能:
1、用户的用户名和密码从一个文件中(hellofunc)读取
2、hellofunc文件包含多个用户名,密码,用户名和密码通过|分割,
每个人的用户名和密码占用文件中一行
3、完成三次验证,三次验证不成功则登陆失败,返回False
4、登陆成功返回True
hellofunc文件内容:
albert|123456
don|456
jack|789
"""
def get_user_pwd():
"""
获取文件中的用户名和密码,并将其存为字典
:return: dict
"""
user_pwd_dict = {}
with open('hellofunc', encoding='utf-8', mode='r') as f:
for line in f:
user, pwd = line.strip().split('|')
user_pwd_dict[user] = pwd
return user_pwd_dict
def login():
user_dict = get_user_pwd()
count = 1
while count < 4:
username = input("请输入用户名: ").strip()
password = input("请输入密码: ").strip()
if username in user_dict and password == user_dict[username]:
print(f'登录成功,欢迎{username}')
return True
else:
print(f'对不起,用户名或者密码错误,请重新输入,还剩{3-count}次机会!')
count += 1
return False
login()