程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python functions - user login function

編輯:Python

""" 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()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved