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!

student = [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"])
avg = (0.10 * homework) + (0.30 * quizzes) + (0.60 * tests)
return avg

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_letter_grade(lloyd))

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

it is giving error like "Oops, try again. get_class_average([alice, lloyd]) returned 91.15 instead of 85.85 as expected"


#2

Hi Man,

I was burning my head on error with Ms Alice.... finally got it. i dunno what i did. Your for review:
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)
float_total=float(total)
avg=float_total/len(numbers)
return avg
def get_average(student):
homework= average(student["homework"])
quizzes= average(student["quizzes"])
tests= average(student["tests"])

weighted_average=homework*0.1 + quizzes*0.3 + tests*0.6
return weighted_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"

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


#3

Hi @kathan420

I had the same problem it's because of the indentation

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

is different to

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

If the return average(result) has the same indentation as the result.append(get_average(n)) it ends the loop at alice.

If it's not in the same indentation the loop continues then eventually does the average(result)

Very annoying but subtle difference


#4

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!

student = [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"])
avg = (0.10 * homework) + (0.30 * quizzes) + (0.60 * tests)
return avg

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_letter_grade(lloyd))

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


#5


#6


#7

THX! It was the *** indentation again.


#8

execute the 'return average(results)' line of code outside the for loop, indentation is key in python :stuck_out_tongue: