如何在 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)
Hello I am a beginner in python and I am trying to make a list with user input. Example: if user wants to add 2 things to the list... say bananas and apples they type 2 for the amount of things they want to add to the list and then type each fruit in. This should add both fruits but I am getting a bug. Here is my code:
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须指定什么是自定义的
You have to specify what is custom