Part of The Whole


#1




Here's the error message I'm getting:
Oops, try again. get_class_average([alice, lloyd]) returned 91.15 instead of 85.85 as expected


Everything worked fine until this, and now its saying the calculation is wrong somewhere.


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)
    total = total / len(numbers)
    return total
    
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
    
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(lloyd))

def get_class_average(students):
    results = []
    for student in students:
        results.append(get_average(student))
        return average(results)


#2

85.85 is the average of another student, where could you possibly be getting that from? (It should only be looking at the argument)


#3

it says its the average of alice and lloyd only.


#4

That's what you should compute, not what your result is

Oh, yeah. I switched them up. 91.15 is the average of another student.

I switched up the numbers, but it's still the same problem, that's the average of ONE of the students


#5

so whats the solution?


#6

You're gonna want to identify where it goes wrong. Print statements could be added to show what it's doing step by step. A high level view would be a good start, like, is it computing the average of each student in the input? Is it then computing the average of those averages?


#7

Added some print lines, here's the code with prints :

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)
    total = total / len(numbers)
    print total
    return total
    


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

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(lloyd))
print get_letter_grade(get_average(alice))
print get_letter_grade(get_average(tyler))

student = [lloyd, alice, tyler]
def get_class_average(students):
    results = []
    for student in students:
        results.append(get_average(student))
        print results
        return average(results)

and here's the results i'm getting in the console :

88.5
74.0
82.5
88.5 74.0 82.5
B
97.5
85.3333333333
93.0
97.5 85.3333333333 93.0
A
46.0
51.0
100.0
46.0 51.0 100.0
C
None


#8

Now getting this error code:
Oops, try again. get_class_average([alice]) returned 80.55 instead of 91.15 as expected


#9

i had the same issue till i realized that the "return" in "get_class_average" stops the "for" loop if it is indented...so just delete one tab in front of return


#10

Thanks i got it to work now!


#11

Looks like you weren't calling your function, so those prints weren't executing :confused: You would want to test with the input that was reported to fail.
(Probably good to comment out the prints you're not currently interested in as well)


#12

Yea i fixed the functions I was calling and the return indent. Those were the issues.


#13

Right. It's just that I'm more interested in finding the issue than in having found it


#14

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.