在for循环中分割数组?

2024-05-20 20:46:57 发布

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

我的老师给了我一个任务,其中一个问题要求我将数组中的所有内容除以26.22(一个完整的马拉松)。我一整天都在做这个,我完全被卡住了。有人能告诉我怎么做吗

这就是我目前所拥有的

import string

forename = []
surname = []
distance = []
farthest_walk = []
marathon = []
#Opening the text file and sorting variables
data = open("members.txt","r")
for line in data:
  value = line.split(',')
  forename.append(value[0])
  surname.append(value[1])
  distance.append(value[2])
#Closing the text file
data.close()

Results = open("Results.txt","w+")
Results.write("The number of whole marathons walked be each member is:\n")
for count in range(len(distance)):
  if float(distance[count])/ 26.22 = temp:
    marathon.append
    Results.write(forename[count]+":")
    Results.write(surname[count]+":")
    Results.write(marathon[count])
Results.close()

它应该以ForenameSurnameWholeMarathosRun结束,但我不知道它如何到达那里


Tags: thetexttxtdatavaluecountopensurname
1条回答
网友
1楼 · 发布于 2024-05-20 20:46:57

你快到了。 对于每个名字,您需要计算他跑了多少马拉松,这可以通过以下操作实现:

 temp = float(distance[count])/ 26.22

这不需要在if语句中

然后,您需要在输出文件中的名称后写入此值:

Results.write(forename[count]+":")
Results.write(surname[count]+":")
Results.write(temp)
# line break such that each result stay in one line
Results.write("\n")

所有这些行都在您已经拥有的最后一个for循环中

相关问题 更多 >