Suz*_*zzy 0 python loops for-loop list
我在将用户输入放入列表时遇到了一些麻烦。我基本上希望用户输入大约 5 件事,并将每个项目单独存储在列表中。然后我想显示该列表中的所有输入。如果有人可以提供任何指导,我们将不胜感激。这是我到目前为止所拥有的:
mylist=[1,2,3,4,5]
print mylist
print"Enter 5 items on shopping list"
for i in mylist:
shopping=raw_input()
print shopping
Run Code Online (Sandbox Code Playgroud)
我强烈建议您通读Python 文档,其中提供了一些列表操作的基本示例 - 打开 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
43775 次 |
| 最近记录: |