8/11 Part of the Whole


#1

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

Oops, try again. get_class_average([alice]) resulted in an error: list indices must be integers, not str


#2

Change students to student in results.append.
should be:
results.append(get_average(student))
hopefully it will work.


#3

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

return result

def get_average(student1):
homework =average(student1['homework'])
quizzes=average(student1['quizzes'])
tests=average(student1['tests'])
a= 0.1 * homework + 0.3 * quizzes + 0.6 * tests

return a

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"
result=[]
def get_class_average(students) :

for curr in students:
    result.append(get_average(curr))
print average(result)
return average(result)

get_class_average([lloyd,alice,tyler])

This shows "Oops, try again. get_class_average([alice]) returned 85.6875 instead of 91.15 as expected" error.


#4

i have the same error and i don't know how to change my code


#5

Oh I got this right

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 0.1 * homework + 0.3 * quizzes + 0.6 * tests
def get_letter_grade(score):
if type(score) == int or type(score) == float:
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"
elif score < 60:
return "F"
else:
print "You should input a number !"
print get_letter_grade(get_average(lloyd))

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


#6

please notice where you code this: " result=[] "


#7
error=Oops, try again. get_class_average([alice]) resulted in an error: get_average() takes exactly 1 argument (3 given)

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

indent preformatted text by 4 spaces


#8

Hi !

I really don't understand wher the problem comes from; it says "'return' outside function", but I'm sure my indentation is correct. Could anyone help, please ?

n


#9

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

What's wrong with it? Please, may anyone help? Thank you!


#10

I had this same error. I think it was because I accidentally indented
return average(results)
Make sure you don't extra indent it. It should line up with for student in students:

for student in students:
        results.append(get_average(student))
return average(results)

#11

Hi its working for me :slightly_smiling:

please find code below

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

numbers = [1,2,3,4,5,6,7,8,9,0]
average(numbers)
print average (numbers)

def get_average(student):

homework = average(student["homework"])
print homework
quizzes = average(student["quizzes"])
print quizzes
tests = average(student["tests"])
print 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"

get_average(student)
a = get_average(student)

print get_letter_grade(a)

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

students = [alice, tyler, lloyd]
print get_class_average(students)