计算平均值及确定最低最高值

0 投票
1 回答
2577 浏览
提问于 2025-04-17 22:34

我把一个包含名字和对应分数的列表拆分开,存储在一个叫做students的字典里。但是现在我需要根据这些分数来计算平均分、最低分和最高分。我是个初学者,所以希望能有一步一步的解释。

def getStatistics(students):

#  Initialize properly the values of the average score, minimum score, and maximum score. 
    average = 0
    lowest = True
    highest = True
    scoreTotal = 0

上面这些变量之所以这样命名,是因为在我的主函数里调用了这个函数,而这个调用里用的就是这些名字。但是每次我运行程序的时候,它都不打印任何内容。

#  loop through the dictionary, and
#  calculate the average score, the highest score and the lowest score
#  of the students in the dictionary.

    i = 0
    for grade in students:
        scoreTotal += grade[i]
        i = i + 1
    average = scoreTotal/i
    lowest = min(grade[i])
    highest = max(grade[i])

#  The function must return back the average, highest and lowest score.
   return average, highest, lowest

在主函数里有这样一段代码:("nameToMarksMap"是前面部分的函数,它使用了students这个字典)

 average, highest, lowest = getStatistics(nameToMarksMap)

 print "Average:", average, "Highest:", highest, "Lowest:", lowest 

什么都没有打印出来,谁能解释一下为什么吗?我一直在遇到错误。

1 个回答

0

完成这个函数,使用下面的代码:

def getStatistics(students)
    ...
    # snip
    ...
    return average, highest, lowest

撰写回答