Python 3.x Python:停止while循环结束

Python 3.x Python:停止while循环结束,python-3.x,function,while-loop,return,break,Python 3.x,Function,While Loop,Return,Break,基本上,我正在开发一个程序,该程序将登录详细信息存储到字典中,如果您的输入与附加的登录详细信息匹配,则允许您登录并调用logged()函数。然而,我的第二个菜单似乎有一个问题,它是logged()函数,它似乎不处理任何输入,而保持它运行的while循环只是中断了,它跳回到再次询问您的登录详细信息(登录详细信息仍然有效)。当在logged()菜单上输入null输入时,它应该说“这不是一个有效的选项,请再试一次”。任何预期的输入都应该调用该函数,但它们仍然会再次跳回询问登录详细信息。这些while循

基本上,我正在开发一个程序,该程序将登录详细信息存储到字典中,如果您的输入与附加的登录详细信息匹配,则允许您登录并调用logged()函数。然而,我的第二个菜单似乎有一个问题,它是logged()函数,它似乎不处理任何输入,而保持它运行的while循环只是中断了,它跳回到再次询问您的登录详细信息(登录详细信息仍然有效)。当在logged()菜单上输入null输入时,它应该说“这不是一个有效的选项,请再试一次”。任何预期的输入都应该调用该函数,但它们仍然会再次跳回询问登录详细信息。这些while循环目前对我来说有点太混乱了,我已经尝试将validintro变量转换为True或False。我们将不胜感激

vault = {}

def 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")
        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 vault[username] == password:
                    print("Username matches!")
                    print("Password matches!")
                    logged() #jumps to logged function and tells the user they are logged on
            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.
        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

#LOGGED ONTO THE PASSWORD AND WEBSITE APP ADDING CONSOLE----------------------------------------------------------------------------------

def logged():
    print("You are logged in!\n")
    modea = input("""Below are the options you can choose:
    ##########################################################################\n
    1) Call test1 function
    2) Call test2 function
    3) Exit
    ##########################################################################\n
    > """).strip()
    return modea    

#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 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"]:
            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":
        test2()

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

print("Goodbye")
vault={}
def菜单():
mode=input(““”Hello{},下面是您可以选择的模式:\n
##########################################################################
a) 使用用户名和密码登录
b) 注册为新用户
要选择模式,请在下面输入模式的对应字母
##########################################################################\n
>“”。格式(名称)).strip()
返回模式
def login():
如果len(vault)>0:#用户必须在请求登录详细信息之前附加用户名和密码
打印(“欢迎使用登录控制台”)
尽管如此:
用户名=输入(“输入用户名:”)
如果用户名==“”:
打印(“未输入用户名,请重试!”)
持续
密码=输入(“输入密码:”)
如果密码==“”:
打印(“未输入密码,请重试!”)
持续
尝试:
如果vault[用户名]==密码:
打印(“用户名匹配!”)
打印(“密码匹配!”)
logged()#跳转到logged函数并告诉用户他们已登录
except-KeyError:#except-KeyError识别列表中是否存在用户名和密码
打印(“未找到输入的用户名或密码!”)
其他:
打印(“您没有存储用户名和密码!”)
def register():#附加用户名的示例。这同样适用于密码
打印(“请在密码库中创建用户名和密码。\n”)
尽管如此:
validname=True
有效名称:
username=input(“请输入要添加到密码库中的用户名。注意:用户名长度必须至少为3个字符:”).strip().lower()
如果不是username.isalnum():
打印(“您的用户名不能为空,不能包含空格或符号\n”)
elif len(用户名)<3:
打印(“您的用户名长度必须至少为3个字符\n”)
elif len(用户名)>30:
打印(“您的用户名不能超过30个字符\n”)
其他:
validname=False
validpass=True
有效通行证:
password=input(“请输入要添加到密码保管库的密码。注意:密码长度必须至少为8个字符:”).strip().lower()
如果不是密码。isalnum():
打印(“您的密码不能为空,不能包含空格或符号\n”)
elif len(密码)<8:
打印(“您的密码长度必须至少为8个字符\n”)
elif len(密码)>20:
打印(“您的密码长度不能超过20个字符\n”)
其他:
validpass=False#validpass必须为True才能保留在函数中,否则如果为False,它将执行另一个操作,在这种情况下,会附加密码。
vault[用户名]=密码
validinput=True
有效输入时:
退出=输入(“\n输入'end'退出,或输入任何键继续添加更多用户名和密码:\n>”)
如果退出[“结束”、“结束”、“结束”]:
返回
其他:
validinput=假
寄存器()
返回寄存器
#登录密码和网站应用程序添加控制台----------------------------------------------------------------------------------
def logged():
打印(“您已登录!\n”)
modea=input(“”)以下是您可以选择的选项:
##########################################################################\n
1) 调用test1函数
2) 调用test2函数
3) 出口
##########################################################################\n
>“”“).strip()
返回模式
#主要程序
打印(“欢迎使用密码保险库程序”)
打印(“在此程序中,您可以将用户名和密码存储在密码保险库中,并在以后查看它们。\n”)
validintro=False
虽然不是validintro:
name=input(“你好,用户,你叫什么名字?:”)
如果len(名称)<1:
打印(“请输入名称:”)
elif len(姓名)>30:
打印(“请输入不超过30个字符的名称:”)
其他:
validintro=True
打印(“欢迎使用密码库程序{}.”格式(名称))
#主程序在while循环中运行,以便程序继续返回程序的菜单部分以获得更多输入,直到用户希望程序停止
validintro=False
虽然不是validintro:
已选择的_option=menu()#将创建一个自定义变量,将菜单函数放入while循环
validintro=False
如果在[“a”,“a”]中选择了_选项:
登录()
如果在[“b”,“b”]中选择了选项:
寄存器()
其他:
打印(“”这不是一个有效选项,请重试:\n“”)
validintro=False
validintro=False
而不是
def logged():
    print("You are logged in!\n")
    keeplooping = True
    while keeplooping:
        modea = input("""Below are the options you can choose:
        ##########################################################################
        1) Call test1 function
        2) Call test2 function
        3) Exit
        ##########################################################################
        > """).strip()

        if modea == "1":
            print("Run Test 1\n")
        elif modea == "2":
            print("Run Test 2\n")
        elif modea == "3":
            keeplooping = False
        else:
            print("That was not a valid option, please try again\n")
    return modea