TypeError:'int'对象不可调用

rob*_*rob 46 python python-2.7

给出以下整数和计算

from __future__ import division

a = 23
b = 45
c = 16

round((a/b)*0.9*c)
Run Code Online (Sandbox Code Playgroud)

这导致:

TypeError: 'int' object is not callable.
Run Code Online (Sandbox Code Playgroud)

如何将输出舍入为整数?

Dav*_*nan 126

代码中的其他地方你有一些看起来像这样的东西:

round = 42
Run Code Online (Sandbox Code Playgroud)

然后当你写

round((a/b)*0.9*c)
Run Code Online (Sandbox Code Playgroud)

这被解释为对绑定到的对象的函数调用round,这是一个int.而那失败了.

问题是无论代码绑定int到名称round.找到并删除它.

  • 总而言之:不要将var和函数命名为相等. (26认同)
  • 啊老`false = true` 问题。这个也抓住了我。蟒蛇是一个狡猾(而且狡猾)的情妇。 (2认同)

小智 10

我遇到了同样的错误(TypeError: 'int' object is not callable)

def xlim(i,k,s1,s2):
   x=i/(2*k)
   xl=x*(1-s2*x-s1*(1-x)) / (1-s2*x**2-2*s1*x(1-x))
   return xl 
... ... ... ... 

>>> xlim(1,100,0,0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in xlim
TypeError: 'int' object is not callable
Run Code Online (Sandbox Code Playgroud)

读完这篇文章后,我意识到我忘记了一个乘号 * 所以

def xlim(i,k,s1,s2):
   x=i/(2*k)
   xl=x*(1-s2*x-s1*(1-x)) / (1-s2*x**2-2*s1*x * (1-x))
   return xl 

xlim(1.0,100.0,0.0,0.0)
0.005
Run Code Online (Sandbox Code Playgroud)

坦克

  • 我知道这是很久以前的事了,但这个答案实际上解决了我的问题。从我这里投票 (2认同)

Ign*_*ams 5

round通过int将其绑定到别处停止踩踏。