【问题标题】:Why is my user-defined function's 'int' object not callable?为什么我的用户定义函数的“int”对象不可调用?
【发布时间】:2019-09-15 02:46:39
【问题描述】:

我创建了一个用户定义的函数,并在尝试打印时不断收到错误消息“TypeError: 'int' object is not callable”

import math
def rangeeee(x, ang, vo, yo):
    fl=(yo+x*math.tan(math.radians(ang))-(1/(2*vo*vo))*((9.8*x*x)/(math.cos**2(math.radians(ang)))))
    return fl

print(rangeeee(1,2,3,4))

【问题讨论】:

  • math.cos**2(math.radians(ang)) 是问题所在。你想在这里做什么?
  • 大概你是想调用 math.cos() 的东西,但你忘记了括号。

标签: python user-defined-functions callable


【解决方案1】:

math.cos() 使用不当:

  • math.cos(math.radians(ang))**2 而不是 (math.cos**2(math.radians(ang)))
    • 注意math.radians(ang)()math.cos() 中的位置
def range_e(x: float, ang: float, vo: float, yo: float) -> float:
    return (yo + 
            x * math.tan(math.radians(ang)) - 
            (1 / (2 * vo**2)) * 
            ((9.8 * x**2) / math.cos(math.radians(ang))**2))

print(range_e(1,2,3,4))

>>> 3.489812396747827
  • 函数已更新为Type annotations(例如range_e(x: float, ang: float, vo: float, yo: float) -> float:
  • 为了便于阅读,方程已分成不同的行
  • 函数已重命名为range_e,因为它比rangeeee 更简单,而且range 会覆盖built-in python function
  • 等式可以是returned,而无需将其分配给f1

【讨论】:

    【解决方案2】:

    我想你想做math.cos((math.radians(ang)*2))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-04
      • 2015-01-30
      • 1970-01-01
      • 2013-07-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多