Part of the whole


#1



why am getting this error??
https://www.codecademy.com/en/courses/python-beginner-en-qzsCL/1/4?curriculum_id=4f89dab3d788890003000096


Oops, try again. Make sure you have defined get_class_average


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):
    sum(numbers)
    total = sum(numbers)
    total = float(total)
    total = float(total) / len(numbers)
    return total
def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])
    get_average = homework * 0.1 + quizzes * 0.3 + tests * 0.6
    return get_average
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 = []
        students = [lloyd, alice, tyler]
        for student in students:
            result.append(get_average(student))
            return average(results)


#2

Check the indention of your get_class_average function.


#3

yes it is.but again am getting errors!!

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

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):
    sum(numbers)
    total = sum(numbers)
    total = float(total)
    total = float(total) / len(numbers)
    return total
def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])
    get_average = homework * 0.1 + quizzes * 0.3 + tests * 0.6
    return get_average
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):
    result = []
    students = [lloyd, alice, tyler]
    for student in students:
        result.append(get_average(student))
        return average(result)

#4

In it's current state, when a list goes through get_class_average, it gets assigned to students because of the parameter. However, you then reassign students to a list that you design. This is hard-coding, which we want to try to stay away from. You want to make your functions as dynamic as they can be.

That's not the error though. The error stems from having the return statement inside of the for-loop. Remember, when a return statement is reached, the function ends right then and there. Meaning that the for-loop will have 1 iteration before stopping.

Hope this helps! if not, feel free to reply and I'll try to go more in-depth.


#5

Sorry ..Can you please explain me more about this.Still i dont know what i did a mistake in that.


#6

Indenting is very important, the return should be outside the loop so that the loop can go through all the students.

When the function is called, it could have a list of [lloyd, alice] or [alice, tyler] or any other combination of the class. However, that value gets stolen when you reassign students to be [lloyd, alice, tyler] inside the function. You want to give functions some room to do the same job for different instances rather than bottle-necking the function to only be able to do handle one instance.


#7

ok i understood..i have cleared the errors.Thank you


#8

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