Float value is not callable


#1

while executing these lines i was getting error message of while calling get_class_average([alice]) the float value is not callable.
i just removed the average variable called the append function directly with get_average(Student)
.can you help me to understand why i cant call it with average or float variable

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

#2

get_class_average() should be called with a list of students (which should be declared and initialized before calling the function), rather than a particular student (both because that's the logical reasoning behind having the function anyway, and because the for loop in the function works with each individual student in the students that is passed through the function)


#3

that error generated because students list had only "alice",and passed to the get_class_average()
we can make any possible combination of students list.
but my main question is why this thing works fine
"""
results.append(get_average(student)) # student from for loop
"""
but not
average=get_average(student)
results.append(average)
. it would be great if you can help.


#4

Ah - I apologize I had misread your question (and also didn't see the [] around alice oops)

Do remember that you have a function much earlier in your code (probably around line 22, right after your initializations for your students) with the name of average as well. Had you renamed the variable at the end of your code to literally anything other than "average" you would be fine, but the compiler gets all confused about which average you're referring to with the code you have right now


#5

Oh yes that would be the reason ... Thanks for helping !!


#6

@shoelaces described the problem well.

You can create a local variable in a function that has the same name as a global one, so long as the function does not depend upon using the global variable.

You have a function named average. But here, you created a local variable named average ...

average=get_average(student)

Here, you try to call the average function, but your get_class_average can no longer access it ...

return average(results)

It is important not to create a local variable that has the same name as a function that you want to call. Programmers often make this type of mistake, not only with the names of functions that they have defined themselves, but also with the names of built in functions. For example, the following would prevent subsequent attempts to access to the built in len function ...

len = 42