[英]Dual password authentication in Python
我在學校里學到了一些python,我一直在嘗試用密碼認證創建一個簡單的登錄系統。
我希望它工作的方式:獲取用戶名和密碼,請求密碼進行身份驗證,如果密碼不正確,請再次詢問,直到找到正確的密碼
到目前為止這是我的代碼:
#online auth
email = input('What is your email address?')
password = input('Enter a password')
passcheck = input('Please re-enter password for confimation')
while passcheck != password:
input('Please re-enter password for confirmation')
else: print("A confirmation code has been emailed to you")
當我運行該程序時,它會正確地詢問電子郵件和用戶名,然后我收到確認問題。 如果我輸入的密碼與我在第一個地方輸入的密碼相同,則進入else語句。 如果我輸入了錯誤的一個,它就會結束一個永不停止的重新輸入密碼的循環,即使我輸入了正確的密碼。
我知道while循環創建了一個無限循環,但我找不到任何好方法來結束它。
你可以做點什么
email = input('What is your email address?')
password_input = False
while not password_input or pass_check != password:
password = input('Enter a password')
pass_check = input('Please re-enter password for confimation')
password_input = True
print("A confirmation code has been emailed to you")
剛剛在朋友的幫助下解決了:
#online auth
email = input('What is your email address?')
password = input('Enter a password')
passcheck = input('Please re-enter password for confimation')
while passcheck != password:
passcheck = input('Please re-enter password for confirmation')
else: print("A confirmation code has been emailed to you")
結束循環的好方法是使用for或counter變量
for index in range(0,5):
password = input('Enter a password')
pass_check = input('Please re-enter password for confimation')
if pass_check == password :
print("A confirmation code has been emailed to you")
index=65532
else :
print("The passwords do not match, try again.")
if index != 65532 :
print("too many attempts, if you want to retry, please restart the program")
注意:65532不是特殊的,它只是超出范圍,所以你退出for。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.