python練習1--使用者登入,python1--登入
1.要求
1)輸入使用者名稱密碼
2)認證成功後顯示歡迎資訊
3)輸錯三次後鎖定
2.需求分析
1)使用者資訊儲存在檔案中(login/config/user_login.txt)
2)使用者輸入使用者名稱和密碼
3)判斷使用者名稱是否存在,存在則繼續,不存在則提示繼續輸入
4)判斷輸入的使用者名稱是否已經被鎖定,如果鎖定則退出程式,否則繼續
5)匹配檔案中的使用者資訊
6)如果匹配則列印出歡迎資訊
7)如果輸入3次密碼錯誤,則鎖定該使用者名稱(login/config/name_lock.txt)
3.測試使用者
bigberg:123abc
lc:123456
smallberg:111111
root:12345
dinasor:12321
1 # -*- coding: UTF-8 -*- 2 #Author:Bigberg 3 4 #定義一個迴圈計數 5 count = 0 6 #定義一個字典儲存使用者名稱和密碼 7 names={} 8 9 #定義一個列表格儲存體使用者名稱10 name_exit = []11 12 #輸入使用者名稱13 username = input("username:")14 15 #判斷輸入的使用者的使用者是否在使用者名稱單中16 with open("../config/name_login.txt",'r') as f:17 while True:18 line = f.readline().strip()19 if not line:20 break21 else:22 name = line.split(':')[0]23 passwd = line.split(':')[1]24 names[name] = passwd25 for key in names:26 name_exit.append(key)27 #判斷使用者名稱是否正確,不正確則繼續輸入28 while username not in name_exit:29 print("The account is not exit.Check it again.")30 username = input("username:")31 else:32 # 讀取鎖定檔案中的內容33 with open("../config/name_lock.txt", "r") as f:34 lock_name = f.read()35 36 # 判斷使用者名稱是否在鎖定檔案中,如果在就退出程式37 if username == lock_name:38 print("Sorry.Your account has been locked.")39 exit()40 else:41 # 3次輸入密碼的機會42 while count < 3:43 passwd_input = input("password:")44 45 # 判斷使用者名稱和密碼是否有效46 if passwd_input == names[username]:47 print("Welcome!", username)48 break49 else:50 print("Error,please try again.")51 52 count += 153 54 # 如果3次輸入密碼錯誤,則將使用者名稱添加到鎖定檔案55 if count == 3:56 with open("../config/name_lock.txt", "w") as f:57 f.write("%s" % username)58 print("You have tried 3 times,and your account will be locked")