Python練習題,如何模擬賬戶只能輸入三次(用戶名和密碼)

for i in range (1,4):
    username=input("Enter your name :")
    if username=="tom":
        print("name is ok")
        break
    else:
        print("name is wrong")
if username=="tom":
    for j in range(1, 4):
        password = input("enter your password")
        if password == "wyl":
            print("password is ok")
            break
        else:
            print("password is wrong")

分析:關鍵是第八行:很多人都想到了利用兩個for循環就可以,解決這個問題.但是忽略了.如果三次用戶名失敗 了,開始運行輸入密碼呀,這在現實生活中不符合邏輯的.所以加入了判定條件,只有你賬戶名對的情況下,纔會進入屬於密碼這一步

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章