python學習:利用循環語句完善輸入設置
阿新 • • 發佈:2018-08-04
then else cati int tro 執行 sed not range
利用循環語句完善輸入設置
使用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學習:利用循環語句完善輸入設置