2017-10-12 5 views
-2
def checked(input, correct): 
if input == correct: 
    return True 
else: 
    return False 

while True: 
    try: 
     user_typed = str(raw_input('Type password\n')) 
     password = str('test') 
     number_of_trys = 0 
    if checked(user_typed, password) is True and number_of_trys <= 3: 
     print('Welcome User') 
     break 
    else: 
     print("Password is incorrect, please try again.") 
     number_of_trys += 1 
     print(number_of_trys) 
finally: 
    print('Loop Complete') 

"number_of_trys"を印刷しようとしましたが、「1」を出力し続けます。そして、はい、私はnumber_of_trys = number_of_trys + 1を試してみました。変数に1を加えて印刷しますが、同じ番号を出力してください。

+0

「number_of_trys = 0」はループ中に変数をリセットしました。その間に外に移動 –

+0

[もっと速い回答を得るために、どのような状況で私の質問に「緊急」や他の類似のフレーズを追加することができますか?](// meta.stackoverflow.com/q/326569) - 要約すると、これはボランティアに対処する理想的な方法ではなく、おそらく回答を得ることには非生産的です。これをあなたの質問に追加しないでください。 – halfer

+0

'def checked'はそれに続く素材よりもインデントされているので、私にとっては間違っています。それが間違っている場合、あなたの質問にこれを解決しますか? – halfer

答えて

0

try:でパスワードを要求するたびに、number_of_trysを0に設定しています。whileループの外でそれを宣言し、 if文

def checked(input, correct): 
    if input == correct: 
     return True 
    else: 
     return False 

number_of_trys = 0 

while True: 
    try: 
     user_typed = str(raw_input('Type password\n')) 
     password = str('test') 
    if checked(user_typed, password) is True and number_of_trys <= 3: 
     print('Welcome User') 
     number_of_trys = 0 
     break 
    else: 
     print("Password is incorrect, please try again.") 
     number_of_trys += 1 
     print(number_of_trys) 
finally: 
    print('Loop Complete') 
+0

助けてくれてありがとう! – purplefuzzkd

関連する問題