2017-06-26 5 views
0

最後に最後の部分を除いてログイン詳細ボールトプログラムを終了しました。Python:else文の後にwhileループを続ける

ユーザーがregister()を使用して辞書にログインの詳細を追加する前にlogin()を呼び出すために「a」を入力してログインオプションにアクセスしようとすると、「ユーザー名またはパスワードが保存されていません! "メニュー()に戻ります。
代わりに、単にメッセージを出力するだけで、私がしたいようにメニュー()に戻るのではなく、プログラムが停止します。

私はこのelse文でmenu関数を呼び出そうとしましたが、これを行うとmain関数のループで実行されるメニュー()に "b"を入力してregister()を呼び出そうとします。 ()関数は呼び出されず、ログインの詳細を追加せずにlogin()を呼び出すのと同じように突然停止します。私の他の解決策は、ループを動作させるように見えましたが、メニューに2度の入力を求めるバグが多くなり、時には何も呼び出さないことがありました。

vault = {} 

def menu(): 
    print("Welcome to the main menu")  
    mode = input("""Hello {}, below are the modes that you can choose from:\n 
    ########################################################################## 
    a) Login with username and password 
    b) Register as a new user 
    To select a mode, enter the corresponding letter of the mode below 
    ##########################################################################\n 
    > """.format(name)).strip() 
    return mode 

def login(): 
    if len(vault) > 0 : #user has to append usernames and passwords before it asks for login details 
     print("Welcome {} to the login console".format(name)) 
     noloop = True 
     while noloop: 
      username = input("Please enter username: ") 
      if username == "": 
       print("Username not entered, try again!") 
       continue 
      password = input("Please enter password: ") 
      if password == "": 
       print("Password not entered, try again!") 
       continue 
      try: 
       if vault[username] == password: 
        print("Username matches!") 
        print("Password matches!") 
        noloop = logged() #jumps to logged function and tells the user they are logged on 
        return noloop 
      except KeyError: #the except keyerror recognises the existence of the username and password in the list 
       print("The entered username or password is not found!") 

    else: 
     print("You have no usernames and passwords stored!") 

def register(): 
    print("Please create a username and password into the password vault.\n") 
    while True: 
     validname = True 
     while validname: 
      username = input("Please enter a username you would like to add to the password vault. NOTE: Your username must be at least 3 characters long: ").strip().lower() 
      if not username.isalnum(): 
       print("Your username cannot be null, contain spaces or contain symbols \n") 
      elif len(username) < 3: 
       print("Your username must be at least 3 characters long \n") 
      elif len(username) > 30: 
       print("Your username cannot be over 30 characters long \n") 
      else: 
       validname = False 
     validpass = True 

     while validpass: 
      password = input("Please enter a password you would like to add to the password vault. NOTE: Your password must be at least 8 characters long: ").strip().lower() 
      if not password.isalnum(): 
       print("Your password cannot be null, contain spaces or contain symbols \n") 
      elif len(password) < 8: 
       print("Your password must be at least 8 characters long \n") 
      elif len(password) > 20: 
       print("Your password cannot be over 20 characters long \n") 
      else: 
       validpass = False #The validpass has to be True to stay in the function, otherwise if it is false, it will execute another action, in this case the password is appended. 
     vault[username] = password 
     validinput = True 
     while validinput: 
      exit = input("\nEnter 'end' to exit or any key to continue to add more username and passwords:\n> ") 
      if exit in ["end", "End", "END"]: 
       return 
      else: 
       validinput = False 
       register() 
     return register 

def logged(): 
    print("----------------------------------------------------------------------\n") 
    print("You are logged in") 

#Main routine 
print("Welcome user to the password vault program") 
print("In this program you will be able to store your usernames and passwords in password vaults and view them later on.\n") 
validintro = False 
while not validintro: 
    name = input("Greetings user, what is your name?: ") 
    if len(name) < 1: 
     print("Please enter a name that is valid: ") 
    elif len(name) > 30: 
     print("Please enter a name with no more than 30 characters long: ") 
    else: 
     validintro = True 
     print("Welcome to the password vault program {}.".format(name)) 

#The main program to run in a while loop for the program to keep on going back to the menu part of the program for more input till the user wants the program to stop 
validintro = False 
while not validintro: 
     chosen_option = menu() #a custom variable is created that puts the menu function into the while loop 
     validintro = False 

     if chosen_option in ["a", "A"]: 
      validintro = not login() 

     elif chosen_option in ["b", "B"]: 
      register() 

     else: 
      print("""That was not a valid option, please try again:\n """) 
      validintro = False 

答えて

1

これが失敗している理由は、(ログインしている)は、ユーザーが存在しない場合、あなたは値を印刷しているが、何も返していません。メッセージを印刷した後で、ログイン機能の最後の行に return Trueを追加します。したがって、ログイン機能は以下のようになります。

def login(): 
    if len(vault) > 0 : #user has to append usernames and passwords before it asks for login details 
    print("Welcome {} to the login console".format(name)) 
    noloop = True 
    while noloop: 
     username = input("Please enter username: ") 
     if username == "": 
      print("Username not entered, try again!") 
      continue 
     password = input("Please enter password: ") 
     if password == "": 
      print("Password not entered, try again!") 
      continue 
     try: 
      if vault[username] == password: 
       print("Username matches!") 
       print("Password matches!") 
       noloop = logged() #jumps to logged function and tells the user they are logged on 
       return noloop 
     except KeyError: #the except keyerror recognises the existence of the username and password in the list 
    print("The entered username or password is not found!") 
    else: 
    print("You have no usernames and passwords stored!") 
    return True 

ループ条件のmain関数の戻り値 'not login()'を比較している理由は以前のとおりです。値が返されないので、Falseがデフォルト値となり、not FalseつまりTrueがValidIntroに格納されます。 Falseで検証すると、これは失敗し、ループは終了しました。

このような場合を避けるには、ループをTrueと比較してください。たとえば、validintroがTrueの場合に実行するループ条件を変更します。

関連する問題