Confused on Part of the Whole


#1

https://www.codecademy.com/courses/python-beginner-en-qzsCL/1/4?curriculum_id=4f89dab3d788890003000096

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

Yeah. I'm not sure why it's getting the correct average. Maybe I've been staring at it too long. Anyone have some advice?

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]
}
classlist = [lloyd, alice, tyler]
# 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"])
    
    final = homework * 0.1 + quizzes * 0.3 +\
    tests * 0.6
    
    return final
    
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 classlist:
        get_average(student)
        results.append(get_average(student))
        

    return average(results)

#2

adding all the numbers gives 82,4. Are you sure you entered the right numbers for alice?


#4

oh wait. thats the number that were already there. I didn't even add those. They were pre-loaded


#5

If I run your code and drop into interactive mode..

$ python2 -i <(xsel -b)    # do magic to run clipboard content
B
>>> average(alice['quizzes'])
85.33333333333333
>>> average(alice['homework'])
97.5
>>> average(alice['tests'])
93.0
>>> get_class_average(alice)
83.86666666666666

Look at how get_class_average manages to get a lower average than all the things it's supposed to average.

You will need to take a closer look at how the function is getting that result. observe what it does.


#6

do you think it's trying to show the class average as just alice's average?


#7

If the class is just alice, then yes, that is what it should do.

I actually called your function incorrectly. Take note of how your function reacted.


#8

well ■■■■. as i'm sure you know its supposed to be lloyd, alice, and tyler. Somewhere i've dun goofed. I think its my tired brain. I appreciate the help though! I'm going to comb through it again and see where i've somehow isolated alice. If I don't find I'll try to look at it again tonight after some sleep.


#9

It's called like this:

get_class_average([alice])

There is no lloyd or tyler there. Just alice.
You can also give it an argument like... "hello, this is dog", what effect would that have? What effect should it have?

get_class_average('hello, this is dog')

#10

I fixed it and I'm not sure if I fixed it the way you were going. Doesn't help that since I made this post I've been editing it a lot. I'll post my "successful code" though.

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]
}
students = [lloyd, alice, tyler]
# 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"])
    
    final = homework * 0.1 + quizzes * 0.3 +\
    tests * 0.6
    
    return final
    
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 students in students:
        get_average(students)
        results.append(get_average(students))
        

    return average(results)
    print average(results)

#11

Why did you change the name of a global variable? Should your function be paying any attention to that? Where should it get its input from? Where were you getting input from?

Is this a well-behaved function?

def add(a, b):
    return c

#12

notable changes that ended up working:

changed

classlist

to

students

and then made the appropriate changes in my ending function. whooo i made that way harder than it should have been.


#13

you mentioned

get_class_average([alice])

not sure where i saw that. even in my original code i posted.


#14

That is what your function was failing to behave correctly for


#15

is there a PM option? Cause I want to pick your brain but not flood this Q&A. I'm not sure if I'm following where you're headed.


#16

Flood all you want, it's perfectly well contained in this thread


#17

Keep it going. I'm interested too.


#18

For the code in the first post of this thread, should the following two return the same value? Do they?

result1 = get_class_average([alice])
result2 = get_class_average([tyler])

#19

Ok then.

So in reply to

That is what your function was failing to behave correctly for

What exactly do you mean? I realize in my original code there was discrepancy between

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

Which I corrected when I changed the global variable. In my mind that was how I fixed it. But you seem to be thinking something different. I'm just hoping for an explanation so I can understand how I should improve this.


#20

I wouldn't think so due to both students having different scores on their relative lists


#21

But they did. And putting in non-sense as the argument would also be the same result. So from that it can be concluded that the function does what with the argument? And if so, how does it produce a result at all?

And you say fixing the global had an effect. Should the function be affected by that? Why?