如何基于单个inpu创建列表

2024-06-01 13:23:45 发布

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

我对编码是新手,从Python开始学习基础知识。通过codecademy和开始我自己的简单的“少年联赛球队规划师”(为我的儿子足球队)

我确实试着研究我的问题,但我找不到解决办法(如果你不知道你在找什么,因为缺乏专业知识的情况下,事情会变得有点棘手)

问题

基于像total\ matches这样的单个输入字段,我想创建一个列表,其中包含每个匹配的列表条目

list_matches = ["match 1", "match 2", ....]

不知道从哪里开始。我想立即打印它们,然后使用一些repeat print语句并使用输入创建一些计数,但是由于我想扩展函数,所以需要将它们放在一个列表中。同时,我也希望能掌握一些技巧,在某个时候,用一个带有日期等的实际匹配列表来交换列表

我就是这样提示匹配的数量的

total_matches = 0
total_matches= int(raw_input("Enter the amount of matches your team has to play: "))

现在我想创建一个列表,在其中我希望

matches = ["match 1", "match 2", ....]

Tags: 编码列表match情况totalmatches基础知识新手
1条回答
网友
1楼 · 发布于 2024-06-01 13:23:45

对于Python 3+,请尝试以下操作:

total_matches= int(input("Enter the amount of matches your team has to play: "))
matches = ['match '+str(k+1) for k in range(total_matches)]

对于python2+,您需要更改从用户获取输入的方式。其余的都一样

total_matches= int(raw_input("Enter the amount of matches your team has to play: "))

相关问题 更多 >