8. Part of the whole


#1

I get the following error when I run my code in the lesson : Oops, try again. get_class_average([alice]) returned 83.8666666667 instead of 91.15 as expected I would love some input from you all!!

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]
}

# Function calculating average number grades
def average(numbers):
    total = sum(numbers)
    total = float(total)
    result = total / len(numbers)
    return result
# Function calculating the total # average of all students
def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])
    total = .10 * average(student["homework"]) + \
    .30 * average(student["quizzes"]) + \
    .60 * average(student["tests"])
    return total
    
#Function calculating the letter grade
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"
    return "F"   
print get_letter_grade(get_average(lloyd))


school_class = (lloyd, alice, tyler)

#Function calculating class average
def get_class_average(students):
    results = []
    for student in school_class:
        results.append(get_average(student))
    return average(results)

#2

your function should work for a class of student of any size (1, 2 or 3 students)

see the function call in the error message? lets add this to your code:

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

print get_class_average([alice])

see how it return the average for 3 students? Not just one?

the answer to solving this mystery has to do with function parameter and your for loop (what you are looping over)


#3

I believe the name of your list with the names of the students should have the same name as the argument in your get_class_average function. Also note that list should use the [ ] brackets and not ( ).


#4

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