8. Part of the Whole


#1
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!

class_list = [lloyd, alice, tyler]

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

I'm getting the error - Oops, try again. get_class_average([alice]) returned 83.8666666667 instead of 91.15 as expected

Anyone know why?


#2

try this

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

new function get_average that gets weighted average of student dictionary

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

Function that returns the grade of a student

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))

Function to get Get class average

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


#3

Thank you. I figured it out.

Instead of
for student in class_list:

I needed to put
for student in students


#4

I can't find anything different from yours in mine, but I'm getting the error:
"Oops, try again. get_class_average([alice, lloyd]) returned 91.15 instead of 85.85 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):
total = sum(numbers)
total = float(total)
total = total/len(numbers)
return total

def get_average(student):
homework = average(student["homework"])
quizzes = average(student["quizzes"])
tests = average(student["tests"])
return (.1 * homework) + (.3 * quizzes) + (.6 * tests)

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"

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


#5

I got the same error, but I solved it when I put "return average(results)" on the same indentation as "for student in students:"

Maybe you made the same mistake as I did and hopefully your problem will also be solved.


#6

I made this mistake, too. Thank you for clarifying.


#7

Thanks so much I did the same!


#8

This 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]
}

class_list = ["lloyd", "alice", "tyler"]

Add your function below!

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

def get_average(student):
homework = average(student["homework"])
quizzes = average(student["quizzes"])
tests = average(student["tests"])
return ((homework*10/100)+(quizzes*30/100)+(tests*60/100))

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"

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

print get_class_average(class_list)

I am getting this error:

Traceback (most recent call last):
File "python", line 55, in
File "python", line 48, in get_class_average
File "python", line 28, in get_average
TypeError: string indices must be integers, not str

Kindly help!!!


#9

I think your error refers to your class_list, in this list you have written the names in str. So this list doesn't recall the dictionaries. If you remove the "- signs your problem will probably be solved.


#10

Thank you so much.. It working like a charm :slight_smile:


#11

I can't figure out what I'm doing wrong. I keep getting an error
"Oops, try again. get_class_average([alice, lloyd]) returned 91.15 instead of 85.85 as expected"

Very frustrating

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]
}

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

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

class_list = [lloyd, alice, tyler] # Don't we need a list of the class to cycle through?? No one seems to have it

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

print get_class_average(class_list)


#12

This one worked for me.Code should look like this:
def get_class_average(students):
results=[]
for student in students:
results.append(get_average(student))
return average(results)


#13

I did the same, but I would like to understand what is happening then. Do you know? Can you explain?