8. Part of Whole - get_class_average([alice]) should return a number ERROR


#1



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


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


I've checked this forum and contrasted it with other codes. I guess I'm not seeing something. Please 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]
}

students = [ lloyd, alice, tyler]

# Add your function below!
def average(numbers):
    total = sum(numbers)
    total = float(total)
    result = total/len(numbers)
    return result
    

def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])
    return 0.3*quizzes + 0.1*homework + 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:
        return results.append(get_average(student))
    return average(results)


#2

a function ends the moment a return keyword is reached

in the loop, why would you return the list? In the loop we are appending to the list, so after the loop we can return the average of the list


#3

Oh! my bad. I didn't notice I had typed return there. Thanks


#4

That solved the issue? Such mistakes are easily made, it can happen :wink:


#5

yes that was all! Got the green button as soon as I made the change. Phew! Thanks!


#6

2 posts were split to a new topic: 8. part of a whole


#8

Hi, writing the following code returns this error: Oops, try again. get_class_average([alice, lloyd]) returned 91.15 instead of 85.85 as expected

My Code:-

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

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)

what do I do?


#9

a function ends the moment a return keyword is reached

if the return keyword is reached in a loop, the function still needs to end, so the loop breaks

so, for your get_class_average, the return keyword is reached in the first iteration of the loop. Is this what you want?


#10

I think this is what we're supposed to do but I'm still sharing the instructions below

Here are the instructions:-

Define a function called get_class_average that has one argument students. You can expect students to be a list containing your three students.
First, make an empty list called results.
For each student item in the class list, calculate get_average(student) and then call results.append() with that result.
Finally, return the result of calling average() with results.


#11

what? Did you read what i told you?

lets say our class consist of 3 students, your get_class_average function would calculate the average grade for the first student and append it to results list

then your function would calculate the class average of the list (for 1 student) and return it, causing the function to end

but you only want to calculate the class average once the average of student is calculated and added to the list. So you need to place return outside of your loop


#12

This was it!
Thank you so much for the explanation
This is going to help me in the future as well. Didn't know all that before
Thanks


#13

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