python 實現功能之:登入

來源:互聯網
上載者:User

標籤:1.3   data-   知識點   不能   user   日期   username   and   str   

一、用 while 迴圈、for 迴圈實現

  1. username = joseph,passwd = 123456
  2. 讓使用者輸入帳號和密碼,輸入使用者和密碼輸入正確的話,提示你  xxx,歡迎登入,今天的日期是xxx,程式結束
  3. 錯誤的話,提示帳號/密碼輸入錯誤,最多輸入3次,如果輸入3次都沒有登入成功,提示失敗次數過多。
  4. 需要判斷輸入是否為空白,輸入空也算輸入錯誤一次
 1 # 使用 for 迴圈 2 import datetime 3 today = datetime.date.today() 4 username = ‘joseph‘ 5 passwd = 123456 6 for i in range(3): 7     get_username = input(‘Please input username:‘) 8     get_passwd = input(‘Please input passwd:‘) 9     if get_username.strip() == str(username).strip():10         if get_passwd.strip() == str(passwd).strip():11             print(‘%s,歡迎登入,今天的日期是%s‘ %(get_username,today))12             break13         else:14             print(‘帳號/密碼輸入錯誤‘)15     else:16         print(‘帳號/密碼輸入錯誤‘)17 else:18     print(‘失敗次數過多‘)

 

 1 # 使用 While 迴圈 2 import datetime 3 today = datetime.date.today() 4 username = ‘joseph‘ 5 passwd = 123456 6 count = 0 7 while count < 3: 8     get_username = input(‘Please input username:‘) 9     get_passwd = input(‘Please input passwd:‘)10     if get_username.strip() == str(username).strip():11         if get_passwd.strip() == str(passwd).strip():12             print(‘%s,歡迎登入,今天的日期是%s‘ %(get_username,today))13             break14         else:15             print(‘帳號/密碼輸入錯誤‘)16     else:17         print(‘帳號/密碼輸入錯誤‘)18     count = count + 119 else:20     print(‘失敗次數過多‘)

 知識點:

  • while...else,for...else 的用法
  • .strip() 的用法

二、增加提示“使用者名稱/密碼不可為空”

 1 import datetime 2 today = datetime.date.today() 3 username = ‘joseph‘ 4 passwd = 123456 5 count = 0 6 while count < 3: 7     get_username = input(‘Please input username:‘) 8     get_passwd = input(‘Please input passwd:‘) 9     if get_username.strip() != ‘‘ and get_passwd != ‘‘:10         if get_username.strip() == str(username).strip():11             if get_passwd == str(passwd):12                 print(‘%s,歡迎登入,今天的日期是%s‘ %(get_username,today))13                 break14             else:15                 print(‘帳號/密碼輸入錯誤‘)16         else:17             print(‘帳號/密碼輸入錯誤‘)18     else:19         print(‘使用者名稱/密碼不可為空‘)20     count = count + 121 else:22     print(‘失敗次數過多‘)

 知識點:

  • 判斷字串為空白:s.strip() == ‘‘

 

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.