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]
}
student = [lloyd, alice, tyler]

Add your function below!

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

def get_average(student):
homework = average(student["homework"])
quizzes = average(student["quizzes"])
tests = average(student["tests"])

def get_letter_grade(score):
#score = float(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:
get_average(student)
results.append(get_average(students))
return average.results

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

I'm not sure why this step doesn't recognize the values of each student as integers whereas each function
I defined in the previous steps worked without incident. Any insight is appreciated.


#2

Change students to student. That should fix it.

Oh, and remove the line above it.


#3

I tried that but I get a different error:

get_class_average([alice]) resulted in an error: 'function' object has no attribute 'results'


#4

Was saving that one for last...

    return average(results)

We have to pass results to the average function. Sorry to keep you in suspense. Where would be the fun if we blurted out every error all at once?


#5

HA! Well played. You are correct. I need to learn as much as I can by doing. Thanks though for the insight.


#6

A post was split to a new topic: get_class_average([alice]) resulted in an error


get_class_average([alice]) resulted in an error
#7

hi @andrewcraven82 and @mtf ,

i've been following your conversation and reviewing my answer,
and i don't see any wrong there, but it said "Oops, try again. get_class_average([alice, lloyd]) returned 91.15 instead of 85.85 as expected"

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

def get_average(student):
homework = average(student["homework"])
quizzes = average(student["quizzes"])
tests = average(student["tests"])
homework *= 0.10
quizzes *= 0.30
tests *= 0.60
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"

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

i've tried to write different code:
def get_class_average(students):
results = []
for student in students:
x = get_average(student)
results.append(x)
return average(results)

but still incorrect :confounded:

please help!

I love codecademy!


#8

You are returning the average(results) in the for loop itself, hence the error remove the indentation and it will get through


#9

Geeezzz haha.. I missed that indentation for more than 30 minutes :sob: :joy:

Soo many thanks!!


#10

As @harshith_hegde said, the return statement is in the function. To help clear things up about what he said, he's saying you need to remove the indentation before the return statement. Therefore if you did it correctly, the return statement should line up with the for loop.

For example:

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

#11

classs=[lloyd,alice,tyler]
def get_class_average(students):
results=[]
for student in classs:
total=get_average(student)
results.append(total)
print results
return average(results)

Oops, try again.
get_class_average([alice]) returned 83.8666666667 instead of 91.15 as expected

need return result only alice? but in instruction said: Finally, return the result of calling average() with results.


#12

Hello,

thank you for your assistance with the challenges that we 'youngsters' face in learning to code.
As with many other learners, I experienced problems with the last function definition. Having eventually got the script to work, I wanted to print the actual output. So I called get_class_average() to the console, with all but the right argument. Everything I input into the parentheses generates an error of some description.

What am I not doing correctly?


#13

Do you have a list of student references?

students = [ lloyd, alice, tyler ]

#16

Hi,
apologies for the delay in getting back - I've been busy!
Yes, I now have the Students array. For some strange reason, it disappeared at some point in the programme, but I couldn't tell you when.
Thank you.