Liz Liz - 2 years ago 361
Python Question

How to store user input into list on Python

I'm having a bit of trouble putting user inputs into a list.
I basically want the user to input about 5 thing and have each item stored in the list individually. I then want to display all the input in said list. If anyone can give any bit of guidance it will be greatly appreciated.
This is what i have so far:


print mylist

print"Enter 5 items on shopping list"

for i in mylist:

print shopping

Answer Source

I strongly urge you to read through the Python docs which provide you with some basic examples of list operation - open up a shell, and type those examples out for yourself. Basically you want to store someone's input into mylist, so there is no need to predefine it with values:


Now you want to prompt a user 5 times (to enter 5 items):

print "Enter 5 items on shopping list"
for i in xrange(5): # range starts from 0 and ends at 5-1 (so 0, 1, 2, 3, 4 executes your loop contents 5 times)
    shopping = raw_input()
    mylist.append(shopping) # add input to the list

print mylist # at this point your list contains the 5 things entered by user
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download