python-------------輸入三次鎖定使用者-----作業

來源:互聯網
上載者:User

標籤:連續   style   bug   logs   使用者名稱   單使用者   open   print   使用者登入   

1,首先要先定義兩個檔案,1個是使用者檔案,1個是黑名單檔案,我是用使用者檔案裡事先定義的使用者名稱和密碼來實現輸入三次錯誤的使用者名稱來鎖定使用者,隨後加入黑名單。重新啟動程     序,不允許這個黑名單使用者登入。

2,這裡的使用者檔案是:“user-”

             黑名單檔案: “Blacklist”

3,這裡只要使用者登入成功,就退出程式,而不是在接著迴圈。

3.有bug,輸入第三次 就鎖定第三次輸入的使用者,而不是一個使用者連續輸入三次 錯誤,才鎖定這個使用者。

4, 代碼如下:

 1 def lock(): 2     ‘‘‘ 3     輸入三次錯誤,鎖定,加入黑名單 4  5     ‘‘‘ 6     conut = 1   #設定一個計數器 7  8     while conut <= 3:   #迴圈不超過三次 9         user_name = input("please your input username : ")10         user_name = user_name.strip()  #去除輸入的空格,分行符號11         f = open("Blacklist",encoding="utf-8")12 13         for line in f :    #遍曆黑名單檔案到line變數中14             if user_name == line.strip():  #判斷輸入的使用者名稱是否在 黑名單中15                 print("bye".center(50,"-"))    #如果在裡面,列印bye,直接退出程式16                 print("您的 %s 使用者已經在黑名單中 !!" %(user_name))17                 exit()   # 退出程式18         user_pwd = input("please your input userpasswold : ")   # 輸入密碼19 20         f1 = open("user-","r",encoding="utf-8")  #  把使用者檔案 存到 f1 變數中21         flag = False  # 設定了一個 開關  預設False  如果 對上了  , 就改為 True22         for line1 in f1:23             user,passwd = line1.strip().split("-----")   # 使用者名稱,密碼 對應 使用者檔案裡的 使用者名稱跟密碼24             if user_name == user and user_pwd == passwd:  #如果 輸入的名字和密碼 等於 使用者檔案裡的 使用者名稱和密碼25                 print("welcome %s ".center(50,"-") %(user_name))   # 執行 輸出 , 之後就退出程式26                 flag = True    #相等的話,就把迴圈外的 flag改成True了27                 exit()28         f1.close()   #關閉檔案29 30         if flag == False:   # 如果 flag還為Flase 說明上面 沒有輸入正確,31             print("sorry %s is not True username or passwold\n" %(user_name))32             conut += 1   #  迴圈+1次33     else:34         print("bye bye".center(50,"-"))35         print("你的 %s 使用者名稱已被加入黑名單" % (user_name))36         f3 = open("Blacklist", ‘a‘,encoding="utf-8")  #再次開啟 黑名單檔案,以"a"追加的方式37         f3.write(user_name)  # 將輸入三次錯誤的使用者加入38         f3.write(‘\n‘)    #換行,使新進來的使用者名稱不跟前面的使用者名稱在一行39         f3.close()  # 關閉檔案40 41 lock()

 

python-------------輸入三次鎖定使用者-----作業

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.