我的老師給了我一項任務,其中一個問題要我將數(shù)組中的所有內(nèi)容除以26.22(A full marathon)。我一整天都在做這件事,完全被困住了,有人可以告訴我如何做這個工作嗎?這是我迄今為止所擁有的import stringforename = []surname = []distance = []farthest_walk = []marathon = []#Opening the text file and sorting variablesdata = 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 filedata.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()它應該作為結(jié)束Forename,Surname,WholeMarathosRun但我看不出它如何能到達那里。
1 回答

子衿沉夜
TA貢獻1828條經(jīng)驗 獲得超3個贊
你快到了。對于每個名字,你需要計算他跑了多少馬拉松,這可以通過以下操作來實現(xiàn):
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您已經(jīng)擁有的最后一個循環(huán)中。
添加回答
舉報
0/150
提交
取消