为什么 get_class_average(class_list) 返回“字符串索引必须是整数”,所有以前的函数似乎都可以正常工作?它应该计算整个班级的平均值,使用以前重用的函数。
lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}
alice = {
"name": "Alice",
"homework": [100.0, 92.0, 98.0, 100.0],
"quizzes": [82.0, 83.0, 91.0],
"tests": [89.0, 97.0]
}
tyler = {
"name": "Tyler",
"homework": [0.0, 87.0, 75.0, 22.0],
"quizzes": [0.0, 75.0, 78.0],
"tests": [100.0, 100.0]
}
def average(lst):
average = float(sum(lst)) / len(lst)
return average
def get_average(student):
score = average(student['homework']) * 0.1 + average(student['quizzes']) * 0.3 + average(student['tests']) * 0.6
return score
def get_letter_grade(score):
if score >= 90:
return "A"
elif 80 <= score < 90:
return "B"
elif 70 <= score < 80:
return "C"
elif 60 <= score < 70:
return "D"
elif score < 60:
return "F"
def get_class_average(class_list):
'''
get_class_average(['lloyd', 'alice', 'tyler'])
'''
total_class = 0
for student in class_list:
get_average(student)
total_class = total_class + get_average(student)
average_class = total_class / len(class_list)
return average_class