2016-08-11 3 views
0

プログラムは、ユーザーに誤ったパスワードの書き込みを許可する必要があります。しかし、パスワードを正しく取得しようとすると、プログラムは「nice」を出力します。ここでパスワードシステム。推測が正しい場合にプログラムを続行できるようにしながら、3つの誤った推測を許可するにはどうすればいいですか?

は、私がこれまで持っているものです:

def main(): 

    n = 2 

    for i in range(0, 3, 1): 

     attempt = input('Enter password: ') 


     if attempt != 'password': 
      print('Incorrect. ' + str(n) + ' attempts left') 

     n = int(n) - 1 

     else: 
      print('nice') 
+2

あなたが探している単語が 'break'です。 – polku

答えて

-1
n = 2 
for i in range(0, 3, 1): 
    attempt = raw_input('Enter password: ') 
    if attempt != 'password': 
     print('Incorrect. ' + str(n) + ' attempts left') 
     n = int(n) - 1 
    elif n > 0: 
     print('nice') 
    else: 
     print('no attempts left') 
+1

このコードは質問に答えるかもしれませんが、このコードが質問に答える理由と理由についての追加の文脈を提供することで、長期的な価値が向上します。コードのみの回答はお勧めできません。 – Ajean

+0

私の意見では、この答えは説明する必要はありません。しかし、とにかく私をdownvote自由に感じる:D –

+1

* Dude *それは私がdownvoted誰かではなかった、と私はあなたを助けようとしていた、あなたは知っている、そのdownvoteを避けていた。 – Ajean

-1
for x in range(3): 
pass_correct=False 
password = "password" 
attempt=raw_input("Enter password: ") 
if attempt == password: 
    pass_correct=True 
    break 
else: 
    print "incorrect, " + str(2-x) + " attempts left" 
if pass_correct: 
    print "nice" 
else: 
    print "no attempts left" 

簡潔

1
max_retries = 3 
for i in range(max_retries): 
    passwd = input('\nEnter password: ') 
    if passwd == 'password': 
     print('\nnice') 
     break 
    print('Incorrect. %d attempts left' % (max_retries-i-1)) 
関連する問題