Saturday, 28 September 2013

CodeAcademy: Just Averages

CodeAcademy: Just Averages

Thanks for your help in advance. I'm trying to get a weighted average of
these students test scores but I get the following error message from
CodeAcademy "Oops, try again! Does your get_average function take exactly
one parameter (a student)? Your code threw a "string indices must be
integers, not str" error." What are they talking about?
Specifically, I'm trying to get the average function to work.
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):
total = 0
for student in students:
total += average(student['homework'])
return float(total) / len(students)
homework_average = get_class_average(students)
def get_average(students):
total = 0
for student in students:
total += average(student['homework'])*.1 + .6*
average(student['tests']) + .3 * average(student['quizzes'])
return (total) /len(students)
print get_average(students)
print homework_average

No comments:

Post a Comment