2017-04-24 8 views
2
users = {'adam' : 'Test123', 'alice' : 'Test321'} 
status = "" 
status = input("If you have an account, type YES, NO to create a new user, QUIT to exit: ") 

while status != 'QUIT': 

    if status == "YES": 
     u_name = input("Please provide your username: ") 
     u_pwd = input("Please provide your password: ") 

     if users.get(u_name) == u_pwd: 
      print("Access granted!") 
      break 

     else: 
      print("User doesn't exist or password error! You have 2 more attempts!") 

    elif status == "NO": 
     print("\nYou're about to create a new user on my very first app. Thank you!") 
     new_u_name = input("Please select a name for your account!") 
     new_u_pwd = input("Please select a password for your account!") 
     users[new_u_name] = new_u_pwd 
     print("Thank you " + new_u_name + " for taking the risk.") 

    elif status == "QUIT": 
     print("Smart choice lol. Please come back in few months") 

をしようと、次を実装する過去の方法だろうか: - ユーザーはYESを選択し、有効なユーザー名+ PWD =アクセス許可された終了ループを提供する場合(私はbreakを使用していますこの場合) - 最初のelseステートメントユーザーの後にもう一度2回だけusernameとpwdを入力するように尋ねられるように、ループを実装する方法はありますか?pythonのユーザー名とパスワードの制限の検証は

答えて

1

私はあなたがカウンターを作成することができますね、のようなもの:

users = {'adam' : 'Test123', 'alice' : 'Test321'} 
status = "" 
status = input("If you have an account, type YES, NO to create a new user, QUIT to exit: ") 
max_attempts = 2 
while status != 'QUIT': 

    if status == "YES": 
     u_name = input("Please provide your username: ") 
     u_pwd = input("Please provide your password: ") 

     if users.get(u_name) == u_pwd: 
      print("Access granted!") 
      break 

     else: 
      if max_attempts > 0: 
       print("User doesn't exist or password error! You have {} more attempts!".format(max_attempts)) 
       max_attempts -= 1 
      else: 
       print("Too many wrong passwords. Bye!") 
       break 

    elif status == "NO": 
     print("\nYou're about to create a new user on my very first app. Thank you!") 
     new_u_name = input("Please select a name for your account!") 
     new_u_pwd = input("Please select a password for your account!") 
     users[new_u_name] = new_u_pwd 
     print("Thank you " + new_u_name + " for taking the risk.") 

    elif status == "QUIT": 
     print("Smart choice lol. Please come back in few months") 

ノート

あなたはまた、次の機能を実装することもできます。

1 - チェックユーザー名がパスワードを要求する前に終了する場合
2 - YESNOケースInsEnsiTive。

+0

ペドロさんありがとうございました。私の遅い返答をお詫び申し上げます。それは魅力のように機能します:)しかし、1つの質問ですが、印刷されたメッセージの試行回数をどのように増やすことができましたか教えてください。 – adam86

+0

あなたは大歓迎です@ adam86。私の答えがあなたを助けたら、投票1 +▲を検討し、投票の真ん中にあるチェックマーク✔をクリックして正しい答えとしてそれを受け入れてください。 –

関連する問題