如何在Python中基于用户输入创建列表



你好,我是python的初学者,我正在尝试用用户输入创建一个列表。示例:如果用户想向列表中添加2个内容。。。比如香蕉和苹果,他们键入2作为要添加到列表中的东西的数量,然后键入每种水果。这应该会添加两种水果,但我遇到了一个错误。这是我的代码:

custom = list()
addresponse = int(input('How many fruits do you want to add to your custom list. Enter Number: '))
for x in range(addresponse):
customresponse = input('What is a fruit you want to add? ')
custom.extend(customresponse)  # Should this be custom.append?
print(custom)

您必须指定什么是自定义

custom = []
addresponse = int(input('How many fruits do you want to add to your custom list. Enter Number: '))
for x in range(addresponse):
customresponse = input('What is a fruit you want to add? ')
#use append method to add value to a list
custom.append(customresponse)
print(custom)

相关内容

  • 没有找到相关文章