2016-05-01 161 views
0

我在将用户输入放入列表中时遇到了一些问题。 我基本上想要用户输入约5件东西,并将每个项目单独存储在列表中。然后我想在所述列表中显示所有的输入。如果任何人可以给任何指导,它将不胜感激。 这是我到目前为止有:如何在Python中将用户输入存储到列表中

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

print mylist 

print"Enter 5 items on shopping list" 

for i in mylist: 
    shopping=raw_input() 

print shopping 
+1

你看过python文档吗? https://docs.python.org/2/tutorial/datastructures.html#more-on-lists – Bahrom

+0

谢谢我使用链接 – Suzzy

回答

2

我强烈建议你通过Python docs它为您提供列表操作的一些基本的例子阅读 - 打开一个shell,然后键入这些例子为你自己。基本上你想别人的输入存入mylist,所以没有必要与价值观预定义的:

mylist=[]

现在要提示用户的5倍(进入5项):

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 
+0

'xrange(5)'或'range(5)'。它从'0'开始,但以'4'结尾。 –

+0

@KlausD。你说得对,对不起,我今晚有点慢。 – Bahrom

相关问题