我一直试图破解这个坚果一段时间。我正在试图找出如何让我的平均值返回浮点数以满足Code Academy的要求。我相信我满足了我的def平均功能的浮动要求。我收到他们的错误消息...... “哎呀,再试一次!确保平均值返回一个浮点数!”
这些是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)
答案 0 :(得分: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
答案 1 :(得分:0)
lloyd = { "姓名":" Lloyd", "作业":[90.0,97.0,75.0,92.0], "测验":[88.0,40.0,94.0], "测试":[75.0,90.0] } alice = { "姓名":" Alice", "作业":[100.0,92.0,98.0,100.0], "测验":[82.0,83.0,91.0], "测试":[89.0,97.0] } tyler = { "姓名":" Tyler", "作业":[0.0,87.0,75.0,22.0], "测验":[0.0,75.0,78.0], "测试":[100.0,100.0]
}
def平均值(数字): 总数=总和(数字) 浮动(总) print total / len(数字)
def average(list): ave = float(sum(list))/ len(list) 回归大道 def get_average(学生): 体重=平均水平(学生["家庭作业"])* 0.1 +平均水平(学生["小测验"])* 0.3 +平均水平(学生["测试"]) * 0.6 回报重量
答案 2 :(得分: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
答案 3 :(得分: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))