如何更新print_quiz_averages功能,以便一次打印每个人的所有分数,而不是显示每个人的每个项目的分数?
from __future__ import division
def main():
quiz_averages = quiz_average(remove_zeros([57,0,36,29,38,31,33,0,42,0,52,28,0,50,26,97,80,63,8,36,33,39,12,36,12,59,75,61,57,39,18,9,19,35,75,5,18,0,56,0,24,0,9],
[83,0,64,45,66,82,52,0,43,0,53,49,43,82,44,74,74,58,52,52,60,18,24,34,19,42,72,65,79,99,75,24,35,70,74,43,55,0,82,0,34,0,22],
[69,0,38,16,16,16,55,0,28,0,44.5,31,0,33,52,75,46,33,36,0,49,34.5,35,79.5,13,27,31,52,40.5,64.5,15,0,31,47,26,0,36,0,68,0,64.5,0,20],
[86,0,95,74,90,53,32,0,79,0,38,63,0,42,61,0,0,70,62,78,0,60,47,89,75,62,84,62,71,80,73,0,31,25,74,0,77,0,90,0,78,0,25]))
student_quiz_scores = {'Raven':[61,65,52,62],
'Jacob':[39,99,64.5,80],
'Jessica':[35,70,47,25],
'Joe':[57,79,40.5,71],
'Aaron':[18,75,15,73]}
print_quiz_averages(student_quiz_scores,quiz_averages)
def quiz_average(all_quizzes):
averages = []
for each_quiz in all_quizzes:
each_average = find_average(each_quiz)
averages.append(each_average)
return averages
def print_quiz_averages(student_quiz_scores,the_averages):
for i, average in enumerate(the_averages):
print "\nThe average for quiz",i+1,"is:",average
for student, student_data in student_quiz_scores.iteritems():
if student_data[i]>average:
print student, "got a",student_data[i],"which is",(str(student_data[i]-average))+"%", "higher than the average. :)"
else:
print student, "got a", student_data[i], "which is", (str(-student_data[i]+average))+"%", "lower than the average. :("
if __name__ == '__main__':
main()
答案 0 :(得分:1)
@adil所说的 - 切换你的嵌套循环。使用上面的代码示例......
for student, student_data in student_quiz_scores.iteritems():
for i, average in enumerate(the_averages):
print "\nThe average for quiz",i+1,"is:",average
if student_data[i]>average:
print student, "got a",student_data[i],"which is",(str(student_data[i]-average))+"%", "higher than the average. :)"
else:
print student, "got a", student_data[i], "which is", (str(-student_data[i]+average))+"%", "lower than the average. :("
当然,如果您希望措辞与评论中的示例完全一致(以及添加空格等),则必须更改部分字符串。