python作業之使用者三次登入及鎖定

來源:互聯網
上載者:User

標籤:代碼   作業   錯誤   使用者密碼   mode   write   break   答案   pytho   

三次登入答案:

使用者密碼檔案:

#file:passwdlulu|123luchuan|123

使用者鎖定檔案:

#file:lockedlulu

代碼:

count = 0  # 輸入三次後確定是否繼續,計數器lock_num = 1  # 三次密碼錯誤後鎖定,計數器while 1:    count += 1  # 計數器    if count <= 3:   # 輸入小於等於3次,則提示輸入帳號和密碼        username = input("pls input username:")        password = input("pls input password:")    elif count > 3:  # 輸入三次後,確認是否需要繼續        confirm = input("確定是否需要繼續! y/n:").strip()        if confirm == "y":            count = 1            username = input("pls input username:")            password = input("pls input password:")        elif confirm == "n":  # "n" 是退出程式            exit("已退出,再見!!!")            username = input("pls input username:")            password = input("pls input password:")    with open("passwd",mode="r",encoding="utf8") as f_read,open("locked",mode="r+",encoding="utf8") as f_write:        # 開啟使用者密碼檔案和鎖定檔案        for lock in f_write:  # 判斷賬戶是否已經被鎖定            lock_user=lock.strip()            if username == lock_user:                print("帳號已被鎖定,請聯絡管理員解鎖")                break        else: # 如果沒有被鎖定,則進行匹配使用者名稱和密碼操作            for line in f_read:                user=line.strip().split("|")[0]  # 使用者名稱                pwd=line.strip().split("|")[1]  # 密碼                if username == user:  # 正確的匹配使用者名稱                    if password == pwd:  # 正確的匹配密碼,顯示登入成功                        print("登入成功")                        lock_num = 0 # 登入成功後,鎖定計數器改為0                        break                    else:  # 密碼輸入錯誤                        print(lock_num)                        if lock_num >= 3:                            f_write.write(user+"\n")                            print("錯誤輸入超過三次,帳號已被鎖定,請聯絡管理員解鎖")                            lock_num = 0 # 鎖定使用者名稱後,計數器改為0                            break                        print("密碼輸入錯誤,請重新輸入")                        break            else:                print("密碼輸入錯誤,請重新輸入")  # 帳號不存在,重新輸入    lock_num += 1

 

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.