Codecademy的蟒蛇 - 学生成为老师 - 8/9

时间:2015-05-18 15:22:22

标签: python

我想很多人都知道python codecademy课程, 我看到有一个特定的部分,很多人都有麻烦, "学生成为老师 - 8/9" 说明有点不清楚, 网上其他人遇到的问题与我的不完全相同......

当我运行此代码时,我得到了正确的答案 - 83.8666666667 (我手动检查了结果), 但我仍然得到一个错误: "哎呀,再试一次。 get_class_average([alice])导致错误:+:' int'不支持的操作数类型和' dict'"

我在这里失踪了什么?

...谢谢

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(numbers):
    total = sum(numbers)
    total = float(total)
    new_total = total / len(numbers)
    return new_total

#print(average(alice["homework"]))

def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])
    return 0.1 * homework + 0.3 * quizzes + 0.6 * tests

#print get_average(tyler)

def get_letter_grade(score):
    if score >= 90:
        return "A"
    elif score >= 80:
        return "B"
    elif score >= 70:
        return "C"
    elif score >= 60:
        return "D"
    else:
        return "F"

#print get_letter_grade(get_average(tyler))


def get_class_average(students):

    return average(students)

results = []
results.append(get_average(lloyd))
results.append(get_average(alice))
results.append(get_average(tyler))

print get_class_average(results)

2 个答案:

答案 0 :(得分:0)

这是我认为你缺少的部分(我也参加了那个教程)。如果你将结果作为def get_class_average(students): results=[] for student in students: results.append(get_average(student)) return average(results) 函数的一部分,我认为你会有更多的运气(例如下面的例子)

subprocess.call

答案 1 :(得分:0)

函数get_class_average([1,2,3,4])获取一个数字列表。当你执行get_class_average([alice])时,你传递的是一个字典列表 当函数尝试将sum应用于dict对象时,如此处的alice,它会抛出该错误 您的程序可以正常运行。例如

>>> sum([{}])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'dict'