2016-09-19 7 views
-1

私は初心者Pythonの学習者だと私は、アカウントのPINを入力するユーザーを求めて、次のコードを書いた:whileループ構文をどのようにフォーマットできますか?

while True: 
    print ('Please enter your pin.') 
    pin = input() 
    if pin =='2356': 
     print('Access granted') 
     break 

...私は、ユーザーが間違ったピンを5回入力することを可能にする場合「アカウントを凍結する」前に、どうすればいいのですか?私は正しいタイプのループを使用していますか?

ありがとうございます!

答えて

0

はい、ほとんどあります。ユーザーが間違ったピンを何回入力したかは、次のように値を増やしてカウントできます。

incorrect_tries = 0 
while incorrect_tries <= 5: 
    print ('Please enter your pin.') 
    pin = input() 
    if pin =='2356': 
     print('Access granted') 
     break 
    else 
     incorrect_tries +=1 
関連する問題