我有一個任務,我必須向用戶詢問 4 位數的密碼。正確的 pin 是 1234。我必須給用戶 3 次才能鎖定他們。我還必須添加一個 break 陳述句。這是輸出的示例:
請輸入您的密碼:1112
不正確。請重新輸入:1112
不正確。請重新輸入:1234
正確的。
我的代碼:
def verify_pin(pin)
if pin == "1234"
return True
else:
return False
tries = 3
while counter < 3:
pin = input("please enter your pin code")
if verify_pin(pin)
print("Correct")
break
elif
print("Incorrect.Please enter again: ")
tries =1
我收到無效的語法。我根本不知道自己在做什么,但我真的很想了解和學習。請幫忙。
uj5u.com熱心網友回復:
您有點接近,但是您的代碼存在一些問題(主要是縮進問題,如評論中所述)。不過,這樣的事情應該可以作業:
desired_pin = '1234'
max_tries = 3
def verify_pin(the_pin):
return the_pin == desired_pin
def main():
tries = 0
while tries < max_tries:
pin = input('please enter your pin code: ')
if verify_pin(pin):
print('Correct')
break
else:
print('Incorrect. Please enter again: ')
tries = 1
else: # Else will run when no `break` statement is run in while loop.
print("I am LOCKIN' you out now!")
if __name__ == '__main__':
main()
示例互動:
please enter your pin code: 111
Incorrect. Please enter again:
please enter your pin code: 222
Incorrect. Please enter again:
please enter your pin code: 123
Incorrect. Please enter again:
I am LOCKIN' you out now!
uj5u.com熱心網友回復:
您犯了一些縮進和語法錯誤。我做了一些更改,請將其與您的代碼進行比較,您可以理解其中的區別
def verify_pin(pin):
if pin == "1234":
return True
else:
return False
tries = 0
while tries < 3:
pin = input("please enter your pin code")
if verify_pin(pin):
print("Correct")
break
else:
print("Incorrect.Please enter again: ")
tries =1
uj5u.com熱心網友回復:
您有語法和縮進問題,請檢查以下代碼
def verify_pin(pin):
if pin == "1234":
return True
else:
return False
tries = 0
while tries < 3:
pin = input("please enter your pin code")
if verify_pin(pin):
print("Correct")
break
else:
print("Incorrect.Please enter again: ")
at = 2 - tries
print("You Have %s More Attempt Remaining" % at)
tries = 1
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/326419.html
