【问题标题】:Dividing an array in a for loop?在for循环中划分数组?
【发布时间】:2019-07-30 01:47:58
【问题描述】:

我的老师给了我一项任务,其中一个问题要我将数组中的所有内容除以 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 结尾,但我不知道它是如何到达那里的。

【问题讨论】:

    标签: python arrays for-loop


    【解决方案1】:

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

     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 循环中。

    【讨论】:

    • 非常感谢这对我很有帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    • 2019-07-18
    • 2018-11-12
    • 1970-01-01
    相关资源
    最近更新 更多