Python TypeError: 'float' 对象不可调用是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43710735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:18:32  来源:igfitidea点击:

TypeError: 'float' object is not callable what does it mean?

python

提问by Rosi .j

from random import uniform

def e(x):
    n=len(x)
    return(sum(x)/n)

    def dmean(e,x,new):
    n=len(x)
    return((e*n+new)/(n+1))

    l=[1,2,3,4,5,6,78]
    e(l)

    for i in range(0,5):
    l.append(uniform(0,10))
    e=e(l)
    d=dmean(e,l,uniform(0,10))

回答by Craig

You have a function named e. The first time through the forloop, you overwrite the function name by creating a variable of the same name e=e(l). The next time through the loop, it tries to call the function, but eis now a floatvalue which can't be called. You can fix this by choosing a different name for your variable:

您有一个名为e. 第一次for循环时,您通过创建同名变量来覆盖函数名称e=e(l)。下一次循环时,它尝试调用该函数,但e现在是一个float无法调用的值。您可以通过为变量选择不同的名称来解决此问题:

for i in range(0,5):
    l.append(uniform(0,10))
    e_value=e(l)
    d=dmean(e_value,l,uniform(0,10))

As you can see, Python doesn't distinguish between variable names and function names, so you have to make sure not to use the same name for a variable and a function.

如您所见,Python 不区分变量名和函数名,因此您必须确保不要对变量和函数使用相同的名称。