11.8 Part of the Whole takes only first in list


Program is running "get_class_average([alice,lloyd])" upon submission but giving me the identical result of "get_class_average([alive])". After some research I realized it is not looping the function for each item in the list but ONLY the first in the list that it is running. I printed the same function the program is running and got the same, but when reversed alice and lloyd it gave me the correct average for lloyd.

How do I convince the "get_class_average()" function to loop for each student in list and append it to results rather than applying the function only to the first listed student? I have tried adding brackets into the get_class_average([function]) and that didn't work, as well as the same in a few other spots just testing things. I've also tried using list() to no effect good or bad it seems.

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 = float(sum(numbers))    
    return total / len(numbers)
def get_average(student):
    hw = average(student["homework"])
    quiz = average(student["quizzes"])
    test = average(student["tests"])

    return sum([hw * 0.1] + [quiz * 0.3] + [test * 0.6])

def get_letter_grade(score):
    if score >= 90.0:
        return "A"
    elif score < 90.0 and score >= 80.0:
        return "B"
    elif score < 80.0 and score >= 70.0:
        return "C"
    elif score < 70.0 and score >= 60.0:
        return "D"
        return "F"

students = [tyler,lloyd,alice]

def get_class_average(students):
    results = []
    for item in list(students):
        return average(results)
        return "none"
print get_class_average([lloyd,alice,tyler])
print get_class_average([alice])
print get_average([lloyd])
print get_class_average([tyler])
print get_class_average(students)


The instructions tell you to call get_class_average(students)

Use "students" not individual names because "students" is a list you have created and your function is designed to average the averages of the students.


I'm confused, I am using "get_class_average(students)" already. It should be referring to the students list, and indeed it does, but it only calculates for the first student on that list which is the main problem. I have already tried moving the order around of students within the variable list students and it always gives values according to the first student listed within that list, and only that first student.


a function ends the moment a return keyword is reached, if a return keyword is reached in a loop, the loop breaks


Had a little trouble until I realized that I hadn't ever taken out the "else" statement from when I had tried an "if" workaround, but your right, my return statement was indented one too far and that's why. I'm so annoyed I didn't know about that (or realize it) before trying all those workarounds. I was stuck on that for more than two days and none of the other threads I read had said anything to help, thanks so much stetim94!


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