999-Python-程式練習題

來源:互聯網
上載者:User

標籤:for迴圈   username   使用者輸入   失敗   hang   zhang   readlines   else   span   

 

1、登入程式練習

 1 Readme = open(‘Readme.txt,‘w‘) 2  3 Readme.write(‘‘‘該代碼為登入系統的小Demo,還有很多不完善的地方。 4 主要功能如下: 5 登入成功,列印歡迎訊息;失敗三次,鎖定使用者(目前只能鎖定第三次輸入錯誤的賬戶,未能實現同一賬戶輸入錯誤三次才鎖定的功能) 6  7 ‘‘‘) 8  9 Readme.close()  #關閉檔案10 11 #產生使用者資訊檔12 user_account = open(‘user_account.txt‘,‘w‘)13 14 user_account.write(‘‘‘zhangsan zhangsan15 alex alex16 eric eric17 ‘‘‘)18 19 user_account.close()  #關閉檔案20 21 #產生空的黑名單檔案22 lock_account = open(‘lock_account.txt‘,‘a‘)  #這裡不能用‘w‘,否則每次重新運行程式後,之前儲存的黑名單使用者會被格式化。23 24 lock_account.close()  #關閉檔案25 26 27 count = 3  #最大嘗試次數28 retry = 1 #當前嘗試次數29 30 while retry <= count :  #嘗試次數不超過最大嘗試次數進入while迴圈31   Username = input("請輸入使用者名稱: “).strip().lower()   #去掉空格和不區分大小寫32 33   #輸入使用者名稱後先進入黑名單判斷34   blacklists = open(‘lock_account.txt‘,‘r‘)     #開啟黑名單使用者檔案35   locked_users = blacklists.readlines()  #按行讀取,結果為列表36 37   for lock_user in locked_users:  #取出列表元素38     if lock_user.rstrip() == Username:39       print("你的使用者已被鎖定,請聯絡管理員處理。")40   41   blacklists.close()  #關閉檔案42   43   if len(Username) == 0:  #檢查輸入是否為空白44     print("輸入不可為空,請重新輸入。")45   else:46     Password = input("請輸入密碼: ”).strip()  #讓使用者輸入密碼47     flag = False48 49     whitelists = open(‘user_account.txt‘,‘r‘)   #開啟白名單使用者檔案50     normal_users = whitelist.readlins()  #按行讀取,結果為列表51     52     for normal_user in normal_users:53       user,pwd = normal_user.rstrip().split()  #取出的元素為使用者名稱和密碼,要將其分割,分別賦值給兩個變數54         if user == Username  and pwd == Password:55           print("歡迎%s登入系統。" %(Username.title())56           flag = True57           break  #跳出當前for迴圈58     59     whiltelists.close()  #關閉檔案60 61     if flag = True:62       break  #跳出while迴圈63     else:64       if retry < 3:  #當第三次嘗試錯誤後,不列印該訊息65         print("使用者名稱或密碼輸入錯誤,請重新輸入。剩餘重試次數%d" %(count-retry))66       retry += 167 else:  #即嘗試次數大於三次後68 69   print("使用者嘗試次數過多,已被鎖定!")70 71   new_lock = open(‘lock_account.txt‘,‘a‘)  #這裡不能用‘w‘模式,否則會格式化之前儲存的黑名單使用者72   73   new_lock.write(‘ ‘.join([‘\n‘,Username])  #使用者名稱(換行)寫入黑名單74 75   new_lock.close()  #關閉檔案
View Code

 

999-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.