让用户将项目添加到列表中?

时间:2016-03-29 03:41:22

标签: python python-3.x

我希望用户能够将项目添加到列表中。 例如,如果我要求用户创建一定长度的购物清单(用户确定长度),我该怎么做?到目前为止,我能够想出的是如何向他们询问他们列表的长度,但现在我已经卡住

lengthOfList = int(input("How many items are in your list? "))

shoppingList = []
for i in range(lengthOfList):
    shoppingList.append(i)

1 个答案:

答案 0 :(得分:-1)

这样的事情怎么样?

shoppingList = []
newItem = input('What is your first item?')
while newItem != '':
    shoppingList.append(newItem)
    newItem = input('What is your next item?')
print(shoppingList)

您可以添加一些print语句来解释如何使用您的系统(使其更加用户友好)