8. Part of the Whole


#1

Hi,

Here is 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(number):
    total = sum(number)
    aver = float(total)/len(number)
    return aver
    
def get_average(student):
    homework = average(student["homework"])
    quizzes = average(student["quizzes"])
    tests = average(student["tests"])
    return 0.1 * average(student["homework"]) + 0.3 * average(student["quizzes"]) + 0.6 * average(student["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(students))
    return average(results)
    
print get_class_average(students)
print get_letter_grade(get_class_average(students))

And here is the error message I'm getting:

What could I have done wrong? :confused:

Thanks.


#2

Hi @muzakir ,

Here, you are passing the entire students list to the get_average function ...

results.append(get_average(students))

Is that what you intended?


#3

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 0.6* average(student["tests"]) + \
0.3 * average(student["quizzes"]) + \
0.1 * average(student["homework"])

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"
elif score < 60:
return "F"

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

print get_class_average(students)

Works like a charm, no issues. I think the problem is that you need to append single student index, not the whole students list at once


#4

It's not working for me:

:confused:


#5

Hi @appylpye,

No, my intention was to get the average for each student, as stated in the instructions:

For each student item in the class list, calculate get_average(student) and then call results.append() with that result.

#6

@muzakir ,

We cannot see the code for the get_class_average function in the screen shot that you included in this post. In your original post, you have the call, get_average(students), instead of get_average(student) in that function.

EDIT: August 13, 2016:
Another problem, which may have raised the error message in the console displayed in your screen shot, could be that your code is missing an assignment to a variable, students, that was to be added in Exercise 3: Put It Together.


#7


well, these screenshots can help you, I was trying few times until the compiler messaged "Way to go!". :smiley:


#8

Turns out that was the problem... :unamused:


#9

Turns out I was missing the backslashes when calculating the averages. Thanks, though.


#10

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