8. Part of the Whole


#1



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


Oops, try again. get_class_average([alice,lloyd)] returned 91.15 instead of 85.85 as expected.


I expected this code to work and do not understand why my students list is only picking up alice and lloyd and not tyler


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!
students = [lloyd, alice, tyler]
def average(numbers):
    total = sum(numbers)
    total = float(total)
    average = total / len(numbers)
    return average

def get_average(student):
    homework = average(student["homework"])
    quiz = average(student["quizzes"])
    test = average(student["tests"])
    homework = homework*0.1
    quiz = quiz*0.3
    test = test*0.6
    sum_total = homework+quiz+test
    return sum_total
    
def get_letter_grade(score):
    if score >= 90:
        return "A"
    elif score >= 80 and score <90:
        return "B"
    elif score >=70 and score <80:
        return "C"
    elif score >=60 and score <70:
        return "D"
    else:
        return "F"
print get_letter_grade(get_average(lloyd))

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


#2

a function ends the moment a return keyword is reached

so, your function ends in the first run/iteration of your loop, which means it will never calculate the correct class average for a class of 2 or more students


#3

@lucki.4
You should try indenting return with the same indentation of for


#4

this is always my problem with the for loops on all the lessons that have the for loop with return statements ive always ran into that error because of having the indent in too far :stuck_out_tongue:


#5

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!
total=0
def average(numbers):
    total= sum(numbers)
    y = float(total)/ len(numbers)
    return y
    
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))
students= [lloyd, alice, tyler]   
def get_class_average(student):
    results= []
    for student in students:
       y= get_average(student)
       results.append(y)
       average(results)
    return average(results)
print get_class_average(students)

pls help


#6

actually its giving error 91.5 is expected u getting 83.....


#7

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

the trick is to loop over the function parameter and not the students list defined outside the function.


#8

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