错误:'float'对象不可迭代 - python

时间:2015-11-19 23:21:49

标签: python

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)

    return total / len(numbers)

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

我不知道我做错了什么。

忘了把我得到的:“错误:'浮动'不可迭代”。

我应该得到的例子:

get_average(alice):91.15。

1 个答案:

答案 0 :(得分:1)

sum内置函数需要数字序列作为一个参数加总。你只给它一个号码。在这种情况下,您根本不需要调用sum函数:

 return homework*  0.1 +\
        quizzes *  0.3 +\
        tests   *  0.6  

或者,正确使用sum

 return sum([homework*  0.1,
            quizzes  *  0.3,
            tests    *  0.6])