-3

我一直试图破解这个坚果一段时间。我试图弄清楚如何让我的平均值返回一个浮点数以满足 Code Academy 的要求。我相信我的 def average 函数满足了浮点数的要求。我从他们那里得到了这个错误信息...... “糟糕,再试一次!确保平均值返回一个浮点数!”

这些是 CodeAcademy 要求:编写一个名为 get_average 的函数,该函数将学生字典作为输入并返回他/她的加权平均值。使用您的平均功能来提供帮助。家庭作业占 10%,测验占 30%,测试占 60%。

我想问题是......我还能在哪里坚持浮动?

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(list):
   return float(sum(list)/(len(list)))

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

def get_class_average(studenti):
    for x in studenti:
    print x
    print get_average(x)

studenti=[alice,tyler,lloyd]
get_class_average(studenti)
4

4 回答 4

1

问题是您的脚本在运行时返回语法错误。

改变 :

def get_class_average(studenti):
for x in studenti:
print x
print get_average(x)

def get_class_average(studenti):
    for x in studenti:
        print x    #note the tab
        print get_average(x) #note the tab spaces

由于此语法错误,您的脚本无法完全运行,因此 CodeAcademy 无法验证您的脚本的结果。

在更正语法错误后,我尝试了您的代码片段,并且能够获得以下输出:

{'quizzes': [82.0, 83.0, 91.0], 'tests': [89.0, 97.0], 'name': 'Alice', 'homework': [100.0, 92.0, 98.0, 100.0]}
91.15
{'quizzes': [0.0, 75.0, 78.0], 'tests': [100.0, 100.0], 'name': 'Tyler', 'homework': [0.0, 87.0, 75.0, 22.0]}
79.9
{'quizzes': [88.0, 40.0, 94.0], 'tests': [75.0, 90.0], 'name': 'Lloyd', 'homework': [90.0, 97.0, 75.0, 92.0]}
80.55
于 2013-09-29T04:16:59.573 回答
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 = { “姓名”:“爱丽丝”,“家庭作业”:[100.0, 92.0, 98.0, 100.0],“测验”:[82.0, 83.0, 91.0],“测试”:[89.0, 97.0] } tyler = {“姓名” :“泰勒”,“家庭作业”:[0.0, 87.0, 75.0, 22.0],“测验”:[0.0, 75.0, 78.0],“测试”:[100.0, 100.0]

}

def average(numbers): total = sum(numbers) float(total) print total /len(numbers)

def average(list): ave = float(sum(list))/len(list) return ave def get_average(student): weight = average(student["homework"])*0.1 + average(student["quizzes"] )*0.3 + average(student["tests"])*0.6 返回权重

于 2014-03-03T18:34:08.693 回答
0

它对我有用

loyd = {
    "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]
}
item = ["homework", "quizzes", "tests"]
students =["lloyd", "alice", "tyler"]

# Add your function below!
def average(numbers):
    total = float(sum(numbers))
    total = total / len(numbers)
    return total

def get_average(student):
    homework = float(0.1 * average(student["homework"]))
    quizzes = float(0.3 * average(student["quizzes"]))
    tests = float(0.6 * average(student["tests"]))
    avg = homework + quizzes + tests 
    return avg
于 2014-06-07T18:19:52.713 回答
0

我已经完成了 Codecedemy,这是我的代码。

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]
}

# Add your function below!
def average(numbers):
    total = sum(numbers)
    total = float(total)
    return total/len(numbers)
def get_average(student):
    homework_ave=average(student["homework"])
    quizzes_ave=average(student["quizzes"])
    tests_ave=average(student["tests"])
    return 0.1 * average(student["homework"]) + 0.3 *                           average(student["quizzes"]) + 0.6 * average(student["tests"])
def get_letter_grade(score):
    if 90 <= score:
        return "A"
    elif 80 <= score:
        return "B"
    elif 70 <= score:
        return "C"
    elif 60 <= score:
        return "D"
    else:
        return "F"
print get_letter_grade(get_average(lloyd))
def get_class_average(students):
    results = []
    for student in students:
        results.append(get_average(student))
    return average(results)
students = [lloyd, alice, tyler]
print get_class_average(students)
print get_letter_grade(get_class_average(students))
于 2016-03-16T21:21:40.570 回答