2017-06-26 1 views
0

Ich habe kürzlich mein Login-Detail-Vault-Programm abgeschlossen, mit Ausnahme eines letzten Teils.Python: Fortsetzung der While-Schleife nach einer else-Anweisung

Wenn der Benutzer versuchen sollte, auf die Anmeldeoption zuzugreifen, indem er "a" eingibt, um login() anzumelden, bevor er Anmeldedaten an das Wörterbuch über register() anfügt, sollte er drucken "Sie haben keine Benutzernamen oder Kennwörter gespeichert! " und kehren Sie zurück zum Menü().
Stattdessen druckt einfach nur die Nachricht und das Programm stoppt, anstatt zurück zum Menü() wie ich wollte.

Ich habe versucht, die Menüfunktion auf diese else-Anweisung, aber wenn ich dies tue, und ich versuche, register() durch die Eingabe von "b" im Menü(), die in einer Schleife in der Hauptfunktion läuft, das Register() Funktion wird nicht einmal aufgerufen und stoppt abrupt, ähnlich wie der Aufruf von login() ohne angehängte Anmeldedaten. Meine anderen Lösungen schienen die Schleife funktionieren zu lassen, aber auf Kosten des Menüs mit mehr Fehlern, wo es zweimal nach Eingabe fragt und manchmal überhaupt nichts anruft.

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 

Antwort

1

Der Grund dies nicht möglich ist, in der Login ist(), wenn keine Benutzer vorhanden sind, können Sie einen Wert drucken, sind aber nicht alles zurück. Fügen Sie return True als letzte Zeile der Login-Funktion nach dem Drucken der Nachricht an. Ihre Login-Funktion sollte also wie folgt aussehen.

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 

Der Grund, es nicht früher arbeiten, ist Ihnen der Rückgabewert vergleichen ‚nicht anmelden()‘ in der Hauptfunktion für Loop-Zustand. Da wir keinen Wert zurückgeben, wird False als Standardwert verwendet und not False dh True wird in ValidIntro gespeichert. Wie Sie es mit False validieren, wurde dieser Fehler und Schleife beendet.

Für die beste Vorgehensweise vergleichen Sie die Schleife mit True, um solche Fälle zu vermeiden. Ändern Sie beispielsweise die Schleifenbedingung, die ausgeführt werden soll, wenn validintro True ist.

Verwandte Themen