Part of the whole


#1


Part of the whole

Oops, try again. get_class_average([alice]) should return a number

I don't know what's wrong in this code. Help me to find out.

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)/ len(numbers)
    return total
def get_average(student) :
    homework=average(student["homework"])
    quizzes=average(student["quizzes"])
    tests=average(student["tests"])
    homework=homework * 0.1
    quizzes=quizzes * 0.3 
    tests=tests * 0.6
    return homework+quizzes+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
    print get_letter_grade(get_average(lloyd))

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


#2

Do you agree with the error message that it should return a number?

If you read your function, would you argue for that it does? How does that argument go?

Or if you can't argue for that it would return a number, and you agree that it won't, then you've found the problem.


#3

from the instructions:

Finally, return the result of calling average() with results

you return results without average()


#4

I gave it like this : return average(results)
But its showing : Oops, try again. get_class_average([alice]) returned 83.8666666667 instead of 91.15 as expected


#5

reads @ionatan answer:

he already covers this problem


#6

Can't understand................


#7

Well you are returning a number now, what I said doesn't apply to this new situation.

Now you're instead returning the wrong number. So that's now a matter of which steps should be carried out to get the right number. I'd start by confirming the math manually and/or adding prints in the code which show what is being done step by step to produce the number that it finally returns.

The input that the error message mentions has just one student in it, so it should get the average for that, then get the average of averages (and there's just one average so it would be unchanged) and return the result.

Your code should roughly end up doing the following for that input:

input is: [alice]
first do: get_average(alice) # 91.15
then: average([91.15]) # 91.15, which is then returned

You may also want to try some other input values for the purpose of debugging, for example if you use two students

input is: [alice, tyler]
first do: get_average(alice) # 91.15
then do: get_average(tyler) # some number, not sure what
then: average([91.15, tyler's average])

#8

yeah.. I understand the concept. But i dont knw how to implement it in program.


#9

Then you'd need to figure out what stops you so that you know what to learn

Seems to me like you should be finding out what is being done differently from what you want. And that's what the suggestion above is for, to identify what is being done differently by printing out step by step what is being done so that you can compare it to what you think should be happening

For example, if you got prints saying something equivalent to:

input is: [alice]
do: get_average(alice) # 91.15
do: get_average(alice) # 91.15
do: get_average(alice) # 91.15
do: get_average(alice) # 91.15
do: get_average(alice) # 91.15

You could say, well, why is it counting alice that many times, it sure shouldn't be doing that, which part of the code decides how many times each thing is counted, that must be wrong and that's what to fix


#10

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