def main():
printIntro() # prints into for user
diveline = processDives() # calls function to read a file
def printIntro():
print("Welcome to the Diver Scoring program. This program will calculate an \n overall score for a diver, based on individual dives.")
#prints intro for user
def processDives():
file_object = open("DiveData.txt", 'r') #opens desired file in read mode
diveline = file_object.readlines() #reads the line of the file and develops it into a string
for line in diveline: #iterates through each line in diveline
calculateDiveScore(line) # calls function to find calculations for dive scores
print("\nThe average score for these dives is") #prints the average for scores
def calculateDiveScore(diveline):
diveline = diveline.split()
diveNum = int(diveline[0]) #defines variable for dive number for each dive
difficulty = float(diveline[1]) #defines variable for difficulty for each dive
scoreList = diveline[2:] # defines variables to create score list for each dive
scoreList.remove(max(scoreList)) #removes max value from scoreList
scoreList.remove(min(scoreList)) #removes min value from scoreList
sumscoreList = sum(float(i) for i in scoreList) # finds the sum of scoreList
finalScore = (sumscoreList * difficulty) *.6 # multiplies difficulty and .6 to find the final value for the diver's scores
print("\nThe diver's score for dive", diveNum, "is", "{0:.2f}".format(finalScore)) # outputs info for user to see which dive has which score
for i in finalScore:
print(sum(i))
main() #calls main to operate first
此程序遍历文本文件中的数字值,以查找多次潜水的分数。我试图找到所有潜水的平均分数。我试图找到平均值的值由变量“finalScore”表示。我尝试迭代for循环中的值但是我得到一个类型错误,说明浮动对象不可迭代。我怎样才能在finalScore下找到这些值的总和和平均值?
这是我用于此程序的文本文件的内容。
1 3.5 5.5 6.0 7.0 6.5 6.5 5.5 7.5
2 2.0 8.0 8.5 8.5 9.0 8.0 8.5 8.0
答案 0 :(得分:0)
sumscoreList = sum(float(i) for i in scoreList)
在这里,sum()
不会创建可迭代的。它只是总结提供给它的值并返回值。该名称也具有误导性,因为它表明该变量是一个列表。