""" Write a function to complete the three landing function : 1、 User name and password from a file (hellofunc) Read 2、hellofunc The file contains multiple user names , password , The user name and password pass | Division , Everyone's username and password occupy a line in the file 3、 Complete three verifications , If the verification fails for three times, the login fails , return False 4、 Login successful return True hellofunc The contents of the document : albert|123456 don|456 jack|789 """
def get_user_pwd():
""" Get the user name and password in the file , And save it as a dictionary :return: dict """
user_pwd_dict = {
}
with open('hellofunc', encoding='utf-8', mode='r') as f:
for line in f:
# strip Remove the line break
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(" Please enter a user name : ").strip()
password = input(" Please input a password : ").strip()
if username in user_dict and password == user_dict[username]:
print(f' Login successful , welcome {
username}')
return True
else:
print(f' I'm sorry , Wrong username or password , Please re-enter , And then there were {
3-count} Second chance !')
count += 1
return False
login()