每當我運行函數來計算高於平均值的分數的百分比,我會得到輸出56.301。正確答案應該是34.平均值是54.8415588235294。分數高於平均值的百分比。這個函數應該接受兩個參數:分數列表和平均值
#This function calls on the data file
def get_meet_scores_from_file():
input_file = open('state_meet.txt','r')
all_around_points = []
aline = input_file.readline()
while aline:
values = aline.split(',')
all_around_points.append(float(values[2]))
aline = input_file.readline()
input_file.close()
return all_around_points
#This function calculates the average.
def average_scores(score_list):
average_of_scores = 0
list_length = len(score_list)
for index in range (list_length):
list_item = score_list[index]
average_of_scores = average_of_scores + (list_item/list_length)
return average_of_scores
# This is the function that is causing the problem.
#I am trying to get the code to count the number of scores above the average [enter link description here][1]so I can continue and with the code to determine the percentage.
def percentage_above_average(score_list,average):
above_average = score_list[0]
for i in range(int(above_average)):
if above_average > average:
above_average = above_average + 1
return above_average
迄今爲止唯一的事情我正在嘗試在函數percentage_above_average中計算高於平均值的分數。我知道它不會計算出百分比。 –