1. 程式人生 > >day05-06 continue語句、while循環

day05-06 continue語句、while循環

valid while http += use https 無限 ini 三次

登錄成功就break,登錄不成功就打印

_user = "xiaoyanzi"
_passwd = "woaini"

pass_authentication = False
for i in range(3):
    username = input("username:")
    passwd = input("passwd:")


    if _user == username and _passwd == passwd:
        print("welcome to %s login"%username)
        pass_authentication = True
        
break else: print("invalid username or passwd !!!") if not pass_authentication: print("不洗腳的小燕子!!!")

技術分享圖片

可精簡為:

_user = "xiaoyanzi"
_passwd = "woaini"


for i in range(3):
    username = input("username:")
    passwd = input("passwd:")


    if _user == username and _passwd == passwd:
        
print("welcome to %s login"%username) break else: print("invalid username or passwd !!!") else: print("不洗腳的小燕子!!!")

技術分享圖片

while循環(無限循環,又稱死循環):

while true:
print("中國人民萬歲!")

輸出結果為n個:

中國人民萬歲!

_user = "xiaoyanzi"
_passwd = "woaini"


while true:  #死循環
    username = input("username:
") passwd = input("passwd:") if _user == username and _passwd == passwd: print("welcome to %s login"%username) break else: print("invalid username or passwd !!!") else: print("不洗腳的小燕子!!!")

將死循環改為有限循環:

_user = "xiaoyanzi"
_passwd = "woaini"

counter = 0
while counter < 3:
    username = input("username:")
    passwd = input("passwd:")


    if _user == username and _passwd == passwd:
        print("welcome to %s login"%username)
        
        break
    else:
        print("invalid username or passwd !!!")

    counter += 1


else:
    print("不洗腳的小燕子!!!")

技術分享圖片

技術分享圖片

三次輸入錯誤之後,是否還想繼續3次,再繼續3次,效果:

_user = "xiaoyanzi"
_passwd = "woaini"

counter = 0
while counter < 3:
    username = input("username:")
    passwd = input("passwd:")


    if _user == username and _passwd == passwd:
        print("welcome to %s login"%username)
        True
        break
    else:
        print("invalid username or passwd !!!")

    counter += 1
   
     if counter == 3:
        keep_going_choice = input ("還想玩麽?[y/n]")
        if keep_going_choice == "y":
            counter = 0
        


else:
    print("不洗腳的小燕子!!!")
    

day05-06 continue語句、while循環