我在第 28 行有一个错误说:
UnboundLocalError: local variable 'total referenced before assignment.
但是,在第 24 行的第 28 行以上引用了总数。我不明白发生了什么。我正在尝试编写的代码是取每个学生的测试、测验和作业分数的平均值。
谢谢你的帮助。
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(some):
return sum(some)/len(some)
students = [lloyd, alice, tyler]
total = 0
def get_class_average(students):
for student in students:
total += get_class_average(students)
return float(total) / len(students)
print get_class_average(students)