Part of the whole, error, alice returning the wrong average


#1



https://www.codecademy.com/courses/python-beginner-en-qzsCL/1/4?curriculum_id=4f89dab3d788890003000096


When i run my code, i get "Oops, try again. get_class_average([alice]) returned 83.8666666667 instead of 91.15 as expected"


I expect to get the green light from codecademy for this lesson, I calculated the average using a real calculator and im recieving the right number, im assuming that when i plug in my results, they are being plugged in with the wrong name? im not sure and so i need some help.


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'])
    
    tsum = (homework * 0.10) + (quizzes * 0.30) + (tests * 0.60)
    
    return tsum
print get_average(lloyd)
print get_average(alice)
print get_average(tyler)

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'


classn = [lloyd, alice, tyler]

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


#2

You used students as your input variable. And that can be anything (can be just Alice, or Alice & Tyler, or all the students). But your loop only goes through the case where it's just everyone.

So if you do

print get_class_average([tyler])
print get_class_average([alice])
print get_class_average([lloyd])

output:
>> 83.8666666667
>> 83.8666666667
>> 83.8666666667

You'll just get 83.8666666667 3 times. Which is exactly the same as:

print get_class_average(classn)

output:
>> 83.8666666667

Check what you are looping through.


#3

oh, thanks for the reply!!!


#4

So i fixed my code by changing

to

So why cant i use students as my parameter as long as i call classn as my argument? sry, i kno you already answered me i was just wondering if you can go into a bit more detail? i would greatly appreciate it :slight_smile:


#5

I think you got confused with what I said. Sorry for not being clear.

I was saying that students is your parameter (your placeholder). While classn is something you can replace students with when you call the function to run a certain scenario (which in this case it's everyone as you have classn = [lloyd, alice, tyler].

I was stating the fact that you are not using your parameter, students in your loop but rather your input list, classn.


#6

OOHHHH i got it now, wow you were a great help thank you so much!!!:smile:


#7

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