Part of a Whole


#1



I'm on the 'Part of the Whole' and my code isn't working correctly! Please Help!


I keep getting and error message that says "Opps, try again. get_class_average([alice]) should return a number"


I have no idea what I did wrong and I have checked the forums and no one seems to be having the same problem. What did I miss???


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)
    adverage = total / len(numbers)
    return adverage
    
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 score >= 90:
        return "A"
    elif score <= 90 and score >= 80:
        return "B"
    elif score <= 80 and score >= 70:
        return "C"
    elif score <= 70 and score >= 60:
        return "D"
    else:
        return "F"

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


#2

I am not sure if this is your main problem but shouldn't adverage be average?


#3

Oh goodness I feel dumb . . .
Thank you so much!


#4

Nay it happens to the best of us! Half my posts on the forums have been me making myself look silly by forgetting a ( or ; somewhere lol Glad to have helped and happy coding! :smiley:


#5

I just tried it but it didn't work!
Here is the updated code but I still can't find anything wrong and I'm getting the same error -

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

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 score >= 90:
return "A"
elif score <= 90 and score >= 80:
return "B"
elif score <= 80 and score >= 70:
return "C"
elif score <= 70 and score >= 60:
return "D"
else:
return "F"

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


#6

Should this be results.append(get_class_average(student)) instead of (get_average(student)) to match your def above?


#7

I tried that and it gives me the error -
"Opps, try again. get_class_average([alice]) results in an error: maximum recursion depth exceeded"


#8

Hmm. Well repost code but I am not even sure what that error means lol I have never heard of it. I will try and research it but this might be the time a little bit more experienced person takes over lol


#9

And also, do you have to print and return results at the end? or do you just need one or the other? might be worth checking out but I doubt thats the error it refers too


#10

https://www.codecademy.com/en/forum_questions/53d83ed152f8632602000b25
Ok this is a very old forum post but I think the first answer to this guy may be what you are looking for to solve the problem.


#11

It worked!!!
But I got a new error . . .
Alice printed out the wrong number. I think I have seen this on a different forum and I will look but I would love your help.
"Opps, try again. get_class_average([alice]) returned 83.66666666667 instead of 91.15 as expected."


#12

I have the same issue...


#13

If you would post your code again if you have not already found a solution :smiley:


#14

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

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 score >= 90:
return "A"
elif score <= 90 and score >= 80:
return "B"
elif score <= 80 and score >= 70:
return "C"
elif score <= 70 and score >= 60:
return "D"
else:
return "F"

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

This is my code it still isn't working . . .
If you guys found a solution I would love to hear it!


#15

ok so I passed this lesson slightly different than you so I will go through my code compared to yours.

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

In stead of doing float and sum separate I did them together to numbers. Instead of returning the average I also returned the total with totals equaling total/len(numbers). Reminds me of how in Pig Latin you had to do new_word = new_word(lens etc.

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

Instead of (student) I used students because in my for statement instead of class_list: I used students: for a student in students instead of a student in student.
Other than those changes it seems like the rest of your code is good to go. Hope that helps.


#16

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

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)


#17

Before the fucntiond I created a list class_list = [lloyd, alice, tyler] and used it in the sentence for student in class_list: instead of students as in the code above. Why exactly is this way wrong?


#18

Thank you all for your replies and I finally figured out the rest of the
code!!! If you are still having problems post your error and we will see if
we can work it.


#19

So my problem is I'm getting the message: "Oops, try again. get_class_average([alice]) returned 80.55 instead of 91.15 as expected" and I have no idea why. I haven't changed any of my other functions up until now, so how is alice returning something unexpected when the code is, near as I can tell, what I need it to be?

Here's my code:


#20

ok i was having the same problem for a while, until i realised that my return statement wasn't intended correctly. In your case, you are calling Class_list which doesn't exist. Try this instead:

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