我正在从Code Academy处理这个问题,我想要的代码是分别返回每个学生的测试,测验和家庭作业平均值。
这就是我现在的代码。
它说错误消息'无效语法'等等。
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]
def get_class_average(students):
for student in students:
total += get_average(student)
return float(total) / len(students)
答案 0 :(得分:2)
你用词典而不是列表来调用get_average()
(我假设你的意思是average()
)。因此sum()
无法使用它。为了获得实际的家庭作业或测验或测试列表,您必须total += get_average(student['homework'])
。
你的tyler字典之后你也错过了}
。这将使python认为def average
位是字典的一部分,但它不能,因此错误。
答案 1 :(得分:1)
看来你错过了
之前的卷曲def average...
应该是
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 ...
同样在get_average_class
中,您没有说明get_average
是什么。我认为它是使用你省略的average
的东西,但如果你的意思是平均而不是你有问题。
答案 2 :(得分:0)
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]
def get_class_average(students):
for student in students:
total += get_average(student)
return float(total) / len(students)
答案 3 :(得分:0)
您忘了在average
功能之前关闭大括号。