如何在 Python 中根据用户输入创建列表

发布于 2025-01-10 01:11:43 字数 486 浏览 2 评论 0原文

你好,我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橙幽之幻 2025-01-17 01:11:44

您必须指定什么是自定义的

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)

You have to specify what is 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文