我是 Python 的初學者,我制作了這個登錄系統,嘗試了很多次。我認為它可以簡化任何人都可以幫忙嗎?
a=int(input("Enter the Password: "))
i=5
if a==1234:
print("ACCESS GRANTED")
while not a==1234:
print(f"INVALID PASSWORD ( {i} times left)")
a=int(input("Enter the Password: "))
i-=1
if a==1234:
print("ACCESS GRANTED")
if i==0:
print("Console has been locked")
break
我試過它來改變列印的數量(“ACCESS GRANTED”),但如果不做錯我就不知道怎么做。
uj5u.com熱心網友回復:
也許是這樣的:
a = 0
i = 6
while not a==1234:
a=int(input("Enter the Password: "))
i-=1
if a==1234:
print("ACCESS GRANTED")
elif i==0:
print("Console has been locked")
break
else:
print(f"INVALID PASSWORD ( {i} times left)")
uj5u.com熱心網友回復:
possibility = 5
while True:
attempt=int(input("Enter the Password: "))
if attempt==1234:
print("ACCESS GRANTED")
break
elif possibility > 0:
possibility -= 1
print(f"INVALID PASSWORD ( {possibility} times left)")
else:
print("Console has been locked")
break
使用更多資訊的名稱
uj5u.com熱心網友回復:
您可以使用 for 回圈而不是 while 回圈。
這是我的答案:
max_tries = 5
correct_password = "1234"
for i in range(1, max_tries 1):
password = input("Enter the password: ")
if password == correct_password:
print("Access granted!")
break
else:
print(f"Wrong password! You have {max_tries - i} more tries.")
if i == max_tries:
print("Console has been locked")
print("Closing program...")
uj5u.com熱心網友回復:
a=int(input("Enter the Password: "))
i=5
while True:
if a==1234:
print("ACCESS GRANTED")
break
elif i==0:
print("Console has been locked")
break
else:
print(f"INVALID PASSWORD ( {i} times left)")
a=int(input("Enter the Password: "))
i=i-1
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/530433.html
標籤:Python循环验证简化
