Part of the Whole Returning Wrong Float


#1



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


I get the following error from the get_class_average function:
get_class_average([alice]) returned 80.55 instead of 91.15 as expected


I expected the function to average the three scores from the individual averages set earlier in the program and return the result.
Any insight would be awesome.


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


#2

Indentation problem..

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

#3

I figured it out. If anyone else has my issue, I indented improperly. Return was running without looping(I think that's the term?) correctly because return ended the function before it included all the students.


#4

Oh, haha. Beat me to it! Thanks, Mr. Help Desk Man.


#5

Here you are overwriting the variable students...
It means that even if students = [John, Joe, Bill, Steeve] and you write:
get_class_average(students), the "computer" will always do get_class_average([lloyd, alice, tyler]) instead of get_class_average([John, Joe, Bill, Steeve])


#6

Yeah, should've posted that I figured that out too. Thanks though!
''' def get_class_average(students):
results = []
for student in students:
results.append(get_average(student))
return average(results) '''
is my final code, for anyone stuck.
EDIT: Crap, I thought I had the markdown right for code, but I guess not. Anyway, should be able to get the gist.


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.