【问题标题】:Calculating Averages and Determining Lowest and Highest Values计算平均值并确定最低和最高值
【发布时间】:2014-03-17 03:32:15
【问题描述】:

我已经拆分了一个包含姓名和相应分数的列表,并将它们存储在一个名为 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”是上一部分中的函数与字典学生)

 average, highest, lowest = getStatistics(nameToMarksMap)

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

什么都没有打印出来,谁能解释一下为什么?我不断收到错误。

【问题讨论】:

  • 这个问题似乎是题外话,因为 OP 在拖钓。
  • “拖钓”?这些天来,这是一个新的“东西”吗?显然,孩子在星期天下午想学点东西……看看她的分数 - 一个小家伙。
  • 你能告诉我们你的名单有两三个学生吗?

标签: python python-2.7 dictionary average


【解决方案1】:

完成函数

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多