python練習1--使用者登入,python1--登入

來源:互聯網
上載者:User

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

 



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.