1

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:

mylist=[1,2,3,4,5]

print mylist

print"Enter 5 items on shopping list"

for i in mylist:
    shopping=raw_input()

print shopping
2

2 Answers 2

5

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:

mylist=[]

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
Sign up to request clarification or add additional context in comments.

2 Comments

xrange(5) or range(5). It starts at 0 but it ends at 4.
@KlausD. you're right, sorry I'm somewhat slow tonight.
2

This is my way of storing input into a list:

list = []
i = 1
while i < 6:
    a = input("Please enter " + str(i) +  " list : ")
    list.append(a)
    i+=1
print(list)

1 Comment

why would you use while with i+=1 instead of for and range?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.