我有28行錯誤說:CodeAcademy班級平均成績
UnboundLocalError: local variable 'total referenced before assignment.
但是總金額超過28行引用上線24我不明白這是怎麼回事。我試圖寫的代碼是將每個學生的測試,測驗和作業分數的平均值。
感謝您的幫助。
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)
注意'UnboundLocalError'只是錯誤,你會之一得到。你還會得到一個'RuntimeError',因爲你在'get_class_average'中有一個永不結束的遞歸循環。 – iCodez