dimanche 20 mars 2016

How can I write an always-on "Quit" option in Python?

I'm learning Python and am writing a basic "user profile manager." This program will be able to add, edit or delete user accounts to/from an existing file that contains saved user accounts. I've got it set up so that the person using the program goes through a series of questions to complete adding, editing or deleting (the whole thing is text based). I'm wondering if there's a way I can have each question be listening for a 'quit' keyword, that would close the user manager program, without having to put in an if statement for 'quit' in each individual question. Here's the code for the delete user ability:

action1 = input("Currently saved users:\n" +
                    # userList is a dictionary containing saved users
                    str(userList.keys()) +
                    "\nEnter the name of the profile you would like to delete.\n"
                    ).lower()

    # Prevent the built in Admin and Guest users from being modified
    while action1 == "guest" or action1 == "admin":
        print("Sorry, this profile cannot be modified. Please try again.")
        action1 = input("Enter the name of the profile you would like to delete.\n").lower()

    # Require the active user's password to complete the deletion process (if active user has a password)
    if user.password != None:
        delpass = input("Please enter your password to complete this action:\n")
        while delpass != user.password:
            delpass = input("Incorrect password for %s. Please try again:\n" %user.username)
    else:
        pass

    # Make sure one more time that the active user is sure about deletion
    action2 = input("Are you sure you want to delete this user profile?\n").lower()

    # Delete the selected user profile (which is action1)
    if action2 == "yes":
        del userList[action1]
        print("User " + action1 + " has been deleted from saved users.")
    else:
        print("Deletion of user " + action1 + " has been cancelled.")

Is there any way I can make it so that you can answer any of the questions with 'quit' and it will close the user manager, without adding an if statement for each individual question? Any help would be greatly appreciated! Thanks!

Aucun commentaire:

Enregistrer un commentaire