Python: 用打印语句停止程序

Python: Stopping the program with a print statement

我想在这里完成我的程序,虽然一切都准备就绪,但对于 def endapp() 函数,我不知道如何让它打印 "Goodbye" 来标记程序的结束程序没有 menu() 功能或任何其他功能,如 login() 再次循环。其他一切都按原样工作,但我不知道如何只用打印消息结束程序。我只需要程序在打印 "Goodbye" 后停止输出任何东西,它到达死胡同,而不是使用 sys.exit() 退出整个应用程序。注意:函数 viewapp()、addapp()、summary() 都将循环回到 logged(),我还没有为此添加代码,而 endapp() 应该根本没有循环,应该以"Goodbye" 并且没有其他函数应该循环返回以再次请求输入。提供代码示例,将不胜感激。

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")
    keeplooping = True
    while keeplooping:
        modea = input("""Below are the options you can choose:
        ##########################################################################\n
        1) Viewapp
        2) Addapp
        3) Summary
        4) Exit
        ##########################################################################\n
        > """).strip()

        if modea == "1":
            viewapp()

        elif modea == "2":
            addapp()

        elif modea == "3":
            summary()

        elif modea == "4":
            keeplooping = False
            print("Goodbye")
        else:
            print("That was not a valid option, please try again: ")
            validintro = False 
    return modea 


def viewapp():
    print("*Insert viewapp*")

def addapp(): 
    print("*Insert addapp*")       

def summary():
    print("*Insert summary*")

#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

我可以建议你做的是: 1st - 在 logged() 函数中:如果用户选择

elif modea == '4' keeplooping = False print('Goodbye') return keeplooping 第二 - 在 login() 函数中:而不是 while 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")
    state = True
    while state:
        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!")
                state = logged() #jumps to logged function and tells the user they are logged on
                return state #return the state of user option (True/False)
        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!")

3rd - 在主程序中 validintro 应该相应地改变

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

这是一个很好的技巧,但它必须有效