【发布时间】:2014-01-07 13:23:21
【问题描述】:
下面的代码应该从学生的字典中获取分数列表并计算学生的平均分数。我收到“TypeError:列表索引必须是整数,而不是浮点数”错误。
alice = {
"name": "Alice",
"homework": [100.0, 92.0, 98.0, 100.0],
"quizzes": [82.0, 83.0, 91.0],
"tests": [89.0, 97.0]
}
# Averege function is given below for calculating avg
def average(lst):
l=float(len(lst))
total = 0.0
#code works till here the error occoured below
for item in lst:
add = int(lst[item])
print add
total+=add
return total//l
print average(alice['tests'])
print alice['tests']
【问题讨论】:
-
你为什么要平均泰勒?
-
@SlaterTyranus 你怎么能平均泰勒? 他是imaginary!
-
抱歉,我给出了一个名为 alice 而不是 tyler 的示例条目
标签: python python-2.7