python學習:利用迴圈語句完善輸入設定

來源:互聯網
上載者:User

標籤:...   次數   lse   font   bsp   python學習   lex   print   The   

利用迴圈語句完善輸入設定

使用for迴圈:

代碼1:
_user = "alex"
_password = "abc123"

for i in range(3):
username = input("Username:")
password = input("Password:")
if username == _user and password == _password:
print("Welcome %s login ..." % _user)
break
else:
print("Invalid username or password !")
代碼2:
_user = "alex"
_password = "abc123"

passed_authentication = False

for i in range(3):
username = input("Username:")
password = input("Password:")
if username == _user and password == _password:
print("Welcome %s login ..." % _user)
passed_authentication = True #真,成立
break
else:
print("Invalid username or password !")

if not passed_authentication:
print("您的輸入次數已經達到上限!") #只有在True的情況下,條件成立
 代碼3:
_user = "alex"
_password = "abc123"

#passed_authentication = False

for i in range(3):
username = input("Username:")
password = input("Password:")
if username == _user and password == _password:
print("Welcome %s login ..." % _user)
#passed_authentication = True #真,成立
break
else:
print("Invalid username or password !")
else:
print("您的輸入次數已經達到上限!")#for迴圈正常結束,就可以執行下面的else語句
 

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.