为什么我的用户定义函数的'int'对象不可调用?

时间:2019-09-15 02:46:39

标签: python user-defined-functions callable

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

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))

2 个答案:

答案 0 :(得分:1)

math.cos()使用不当:

  • math.cos(math.radians(ang))**2而非(math.cos**2(math.radians(ang)))
    • 请注意math.radians(ang)math.cos()()内的位置
    • enter image description here
    • enter image description here
    • enter image description here
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

答案 1 :(得分:0)

我认为您想做math.cos((math.radians(ang)*2))