Josh Lin
Josh Lin

Reputation: 3

how to store all users input in the list?

I am having a trouble of making a while-loop to store all inputs in the list and break the loop while it inputs "quit" and then show ALL the inputs that users have done. my codes are as followings but it only shows ["quit"] after all.

while True:                                 
    list_names = []                         
    enter_names = input("what's the name  ")
    list_names.append(enter_names)          
    if enter_names == "quit":               
        break                               
    print(list_names)     

Upvotes: 0

Views: 47

Answers (1)

TysonU
TysonU

Reputation: 432

You main problem is that you need to make the list before the loop. Otherwise it will be recreated each time it loops. It's also a good habit to format the input as well.

#Need to create list before loop
list_names = []
while True:
    names = str(raw_input("what's the name  "))
    list_names.append(names)
    if names == "quit":
        break
print(list_names)

Upvotes: 2

Related Questions