我需要创建一个代码,学生的姓名和分数存储在Python 2.7.11的字典中。我需要计算每个学生的平均分数并打印出来。以下是我到目前为止的情况:
dict_grade= {'Alice':[68,70,90], 'Chris':[95,90,87], 'Cindy':[89,80,75], 'Adam':[75,70,69], 'Richard':[81,80,93], 'Ryan':[51,62,70], 'Edward':[88,90,94], 'Larry':[93,95,97], 'Seth':[67,70,80]} average_grade= {name:averageScore} def avg(dict_grade): return sum(dict_grade)/float(len(dict_grade)) for k, v in dict_grade.values(): print 'Name: ', k, 'and the average score is: ', v
任何帮助都将非常感谢!我知道这是非常简单的事情。谢谢:)
答案 0 :(得分:0)
应该是:
dict_grade= {'Alice':[68,70,90], 'Chris':[95,90,87], 'Cindy':[89,80,75], 'Adam':[75,70,69], 'Richard':[81,80,93], 'Ryan':[51,62,70], 'Edward':[88,90,94], 'Larry':[93,95,97], 'Seth':[67,70,80]}
def get_avg(record):
return sum(record)/ float(len(record))
for k, v in dict_grade.items():
avg_score = get_avg(v)
print 'Name: ', k, 'and the average score is: ', avg_score