让用户向列表中添加项目?

2024-04-26 06:15:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望用户能够添加项目到一个列表。 例如,如果我要求用户创建一个特定长度的购物列表(用户决定长度),我该怎么做?到目前为止,我能想到的只是如何问他们名单的长度,但现在我被卡住了

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

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

Tags: 项目用户in列表inputyouritemsshoppinglist
1条回答
网友
1楼 · 发布于 2024-04-26 06:15:51

像这样的怎么样?你知道吗

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

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

相关问题 更多 >