Search code examples
pythonlistpython-3.xraw-input

Saving User Inputs into a List in Python 3.5.1


I want to prompt the user for a list of phrases over and over again until they input END and save all of the user's inputs into a list. How can I do that?

My code so far:

print("Please input passwords one-by-one for validation.")
userPasswords = str(input("Input END after you've entered your last password.", \n))
boolean = True
while not (userPasswords == "END"):

Solution

  • One way to do is with a while loop:

    phraseList = []
    
    phrase = input('Please enter a phrase: ')
    
    while phrase != 'END':
        phraseList.append(phrase)
        phrase = input('Please enter a phrase: ')
    
    print(phraseList)
    

    Result:

    >>> Please enter a phrase: first phrase
    >>> Please enter a phrase: another one
    >>> Please enter a phrase: one more
    >>> Please enter a phrase: END
    >>> ['first phrase', 'another one', 'one more']