【问题标题】:TypeError: 'float' object is not callableTypeError:“浮动”对象不可调用
【发布时间】:2011-10-19 06:37:51
【问题描述】:

我正在尝试在以下等式中使用数组中的值:

for x in range(len(prof)):
    PB = 2.25 * (1 - math.pow(math.e, (-3.7(prof[x])/2.25))) * (math.e, (0/2.25)))

当我运行时,我收到以下错误:

Traceback (most recent call last):
  File "C:/Users/cwpapine/Desktop/1mPro_Chlavg", line 240, in <module>
    PB = float(2.25 * (1 - math.pow(math.e, (-3.7(prof[x])/2.25))) * (math.e, (0/2.25)))
TypeError: 'float' object is not callable

这可能很简单,但我不太明白。任何帮助都是 不胜感激。提前致谢

【问题讨论】:

  • 您在这里缺少一个操作:-3.7(prof[x]) 它应该是 -3.7 * (prof[x]) 或 ...
  • 我在一个类中遇到了同样的异常,我有一个浮点属性和一个同名的方法。重命名两者之一为我解决了它

标签: python math types


【解决方案1】:

缺少一个运算符,可能是*

-3.7 need_something_here (prof[x])

“is not callable”的出现是因为括号 - 并且缺少将括号转换为优先运算符的运算符 - 使得 Python 尝试 调用 -3.7(浮点数)作为函数的结果,这是不允许的。

在这种情况下也不需要括号,以下可能足够/正确:

-3.7 * prof[x]

正如 Legolas 指出的,还有其他一些问题可能需要解决:

2.25 * (1 - math.pow(math.e, (-3.7(prof[x])/2.25))) * (math.e, (0/2.25)))
                                  ^-- op missing
                                                    extra parenthesis --^
               valid but questionable float*tuple --^
                                     expression yields 0.0 always --^

【讨论】:

  • 请注意,右括号也比左括号多,第二个逗号似乎已关闭。
【解决方案2】:

您忘记了-3.7(prof[x]) 之间的*

因此:

for x in range(len(prof)):
    PB = 2.25 * (1 - math.pow(math.e, (-3.7 * (prof[x])/2.25))) * (math.e, (0/2.25)))

另外,似乎缺少(,因为我数了6次(和7次),我认为(math.e, (0/2.25))缺少一个函数调用(可能是math.pow,但这只是一个猜测)。

【讨论】:

  • 谢谢legolas 和其他所有人,我注意到我的方程式中有几个错别字。也许我需要休息一下,让我的眼睛休息一下:-)
  • 请不要抄袭别人的答案
【解决方案3】:

问题在于-3.7(prof[x]),它看起来像一个函数调用(注意括号)。只需使用 * 这样的 -3.7*prof[x]

【讨论】:

  • 请不要抄袭别人的答案。
猜你喜欢
  • 2011-12-28
  • 2013-11-28
  • 1970-01-01
  • 1970-01-01
  • 2014-03-24
  • 1970-01-01
相关资源
最近更新 更多