8. part of the whole


#1


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


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


#2

The loop header should be ...

    for student in students:

Remove this entirely, so you can instead use student to access each dictionary ...

        get_average(lloyd) 
        get_average(alice)
        get_average(tyler)

Then, instead of this ...

        return results.append()

... append the average for each student to results ...

        results.append(get_average(student))

Make sure the final and only remaining return statement is outside the loop.


#3

Thank you appylpye, I tried the steps you mentioned but am still getting an error message of "Oops, try again. get_class_average([alice]) resulted in an error: list indices must be integers, not str"

What do I need to fix? Thanks!

Add your function below!

def average(numbers):
total = sum(numbers)
total = float(total)
average = total / len(numbers)
return average

def get_average(student):
homework = average(student["homework"])
quizzes = average(student["quizzes"])
tests = average(student["tests"])
return homework * 0.1 + quizzes * 0.3 + tests * 0.6

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):
students = [lloyd, alice, tyler]
results = []
for student in students:
get_average(lloyd)
get_average(alice)
get_average(tyler)
results.append(get_average(students))
return average(results)


#4

In the get_class_average function ...

  • Remove this because it overwrites the students parameter ...

    students = [lloyd, alice, tyler]
  • Remove all of this, because it serves no purpose ...

        get_average(lloyd) 
        get_average(alice)
        get_average(tyler)
  • Do not attempt to average all of the students here ...

        results.append(get_average(students))
  • Instead, append each individual student average ...

        results.append(get_average(student))

#5

I don't have a question about the code itself, because I get the message that everything is ok and I can move to the next step. But I like to see the actual results before I move on to the next step. I've discovered a lot of errors by making sure the code gives the expected result, even though the program tells me it's OK to move to the next step.

For this question, I'm trying the command "print results" and I get an empty list. What I expected to see was the average results for the three students.

If I try print average(results) I get an error code.

How do I see the list of results to make sure the append code is working as expected?


#6

Never mind, I see I jumped the gun on the next problem.


#7

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