8. Part of the Whole - Help Needed


#1



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


I'm receiving the following error message: "Oops, try again. get_class_average([alice]) returned 83.8666666667 instead of 91.15 as expected."

Absolutely no idea why this is happening, or why it can get through lloyd only to get hung up on alice, although that seems to be a common theme. Any help or advice would be greatly appreciated.


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


#2

the function should work for a class of any size, so don't overwrite the function parameter, overwriting the function parameter will mean your function only works for a class with 3 students, and the argument you pass into the function call is irrelevant.


#3

Thanks!!! I removed the students = [...]. I am an absolute rookie when it comes to any programming language, and this is some black magic python voodoo. Why does the code know to look specifically at tyler et. al. just from the line "for student in students"? Until then, we hadn't used the term "students" anywhere.


#4

yes, we did here:

def get_class_average(students):

students is the function parameter, basically a function parameter is a placeholder until you call the function:

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

# function call with different argument values
print get_class_average([lloyd])
print get_class_average([lloyd, alice])
print get_class_average([lloyd, alice, tyler])

see comments in code (i am going to use those terms), when we call the function, the argument value get copied into/passed along to the function parameter

Although you learned this in the python course, its tricky because now your implementing it.

Its important when you create a function and give it a parameter, you reflect on what this parameter represent and what possible values you can give the parameter at function call, so you understand how to use the parameter inside the function (in this case looping over the function parameter because it contains a list value)

Always reflect what you do, and asks yourself why you take certain step.


#5

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