2 回答

TA贡献1886条经验 获得超2个赞
您正在尝试将字符串和列表与浮点数相乘,这是不可能的。
student[item] * weight
尝试这样的事情:
def get_average(student):
weight = 0
total = 0
for item,val in student.items(): #use dict.items() if you need to wrk on both key and values
if item == "homework":
weight = .1
elif item == "quizzes":
weight = .3
elif item == "tests":
weight = .6
else:
continue # no need of weight = 0 simple move on to next item
# continue statement jumps the loop to next iteration
total += (float(sum(val)) / len(val)) * weight
return total
print get_average(tyler) #prints 79.9

TA贡献1785条经验 获得超8个赞
由于您无法将列表乘以权重,因此请先获取平均值!在for循环中添加以下行:
averaged = sum(student[item])/float(len(student[item]))
total += averaged * weight
所以现在这是您的for循环:
for item in student:
if item != "Name":
averaged = sum(student[item])/float(len(student[item]))
if item == "homework":
weight = .1
elif item == "quizzes":
weight = .3
elif item == "tests":
weight = .6
else:
weight = 0
total += averaged * weight
添加回答
举报