jeudi 22 juin 2017

Python: Program jumping to another function without asking for input

Still starting out here soz, I've been working on trying make a program that asks for username and passwords, and trying to connect two menus together and have the first menu give an option to add login details and another option that allows the user to log in with a username and password that is added to the dictionary to be accepted and logged in in order to proceed to the second menu. While the login console itself is doing its job by recognising login details in the list when used in another program, when I call the login option either with login details appended or no details appended at all, it would jump straight to the next menu without asking for login details. If I were to try to log in without details at all, it should give the message "You have no usernames or passwords stored!" and loop back to the menu(), but also jumps to the next menu anyway, which is logged() and logs in without any login details entered. Sorry for the long code, had to show this much for it to make sense. Help would be appreciated.

store = {}

def menu(): 
    mode = input("""Hello {}, below are the modes that you can choose from:\n
    ##########################################################################
    a) Login with login details
    b) Register login details
    To select a mode, enter the corresponding letter of the mode below
    ##########################################################################\n
    > """.format(name)).strip()
    return mode

def login():
    if len(store) > 0 : #user has to append usernames and passwords before it asks for login details
        print("Welcome to the login console")
        while True:
            username = input ("Enter Username: ") 
            if username == "":
                print("User Name Not entered, try again!")
                continue
            password = input ("Enter Password: ") 
            if password == "":
                print("Password Not entered, try again!")
                continue
            try:
                if store[username] == password:
                    print("Username matches!")
                    print("Password matches!")
                    logged() #jumps to logged function and tells the user they are logged on
                    break 
            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(): #example where the username is appended. Same applies for the password
    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 \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.
        store[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"]:
                menu()
                break
            else:
                validinput = False
                register()
        return register

def logged():
    print("----------------------------------------------------------------------\n")
    print("Welcome to menu test")
    modea = input("""Below are the options you can choose from in the menu test:
    ##########################################################################\n
    1) Test1 (Simply prints test1)
    2) Exit (Exits the program)
    Enter the corresponding number to select option
    ##########################################################################\n
    > """).strip()
    return modea

def test1():
    print("Test1")


#Main routine
print("Welcome 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("Hello user, what is your name?: ")
    if len(name) < 1:
        print("Please enter a name: ")
    elif len(name) > 30:
        print("Please enter a name no more than 30 characters: ")
    else:
        validintro = True
        print("Welcome {}.".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 = True

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

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

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

validintro = False 
while not validintro: 
    option = logged()
    print(option) 
    if option == "1":
        test1()

    elif option == "2":
        break
    else:
        print("That was not a valid option, please try again: ")
        validintro = False 

print("Goodbye") 

Aucun commentaire:

Enregistrer un commentaire