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循環就可以,解決這個問題.但是忽略了.如果三次用戶名失敗 了,開始運行輸入密碼呀,這在現實生活中不符合邏輯的.所以加入了判定條件,只有你賬戶名對的情況下,纔會進入屬於密碼這一步