while True:
print('enter username: ')
username = input()
if username.lower() != 'joe':
print("imposter!")
continue
print(f'Hello {username.capitalize()}')
print('enter password: ')
password = input()
tries = 0
if password != 'Water':
tries = 1
continue
if tries == 3:
print("3 strikes, you're out")
quit()
else:
break
print("access granted")
嘗試提示用戶名和密碼。我正在嘗試對用戶名條目進行無限嘗試,并且只有 3 次輸入正確密碼的機會。當您輸入正確的用戶名和密碼時,一切正常。但是當輸入錯誤的密碼時,它會回圈輸入用戶名,并且“嘗試”計數器不起作用。 python noob 嘗試使用 Automate The Boring Things in Python 學習
uj5u.com熱心網友回復:
您可以嘗試重組您的代碼,如下所示:
import sys
access = False
while not access:
username = input('Enter username: ')
if username.lower() != 'joe':
print("imposter!")
continue
else:
print(f'Hello {username.capitalize()}')
for i in range(3):
password = input('Enter password: ')
if password == 'Water':
access = True
break
else:
print("3 strikes, you're out")
sys.exit()
print("Access granted")
uj5u.com熱心網友回復:
tries = 0您在回圈內重置。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/486712.html
