【问题标题】:Python - TypeError: can't convert complex to floatPython - TypeError:无法将复数转换为浮点数
【发布时间】:2017-03-20 07:54:09
【问题描述】:

我需要一些关于学校项目代码的帮助。

import math

v = 9.412
g = 9.81
y = -1.5

def radToDeg(x):
    return((x / math.pi) * 180)

def sqrt(x):
    return(x ** 0.5)

def CalculateAngle(x):
    return(radToDeg(math.atan(((v ** 2) + (sqrt((v ** 4) - (g * ((g * x ** 2) + (2 * y * v ** 2)))))) / (g * x))))

print(CalculateAngle(90.0297))

当我运行程序时,我收到一个错误:

Traceback (most recent call last):
  File "C:/Users/Owner/Desktop/ballista.py", line 16, in <module>
    print(CalculateAngle(float(90.0297)))
  File "C:/Users/Owner/Desktop/ballista.py", line 14, in CalculateAngle
    return float(radToDeg(math.atan(((v ** 2) + (sqrt((v ** 4) - (g * ((g * x ** 2) + (2 * y * v ** 2)))))) / (g * x))))
TypeError: can't convert complex to float

有人可以帮我解决这个问题吗?谢谢!

【问题讨论】:

  • 您的函数似乎设置为接受弧度作为输入,但您正在传递度数...

标签: python types typeerror


【解决方案1】:

参考这个ValueError: negative number cannot be raised to a fractional power

这可能是由于提高幂优先于一元减号。

【讨论】:

    【解决方案2】:

    1) 您正在调用一个名为radToDeg 的函数,其输入角度以度为单位。确保您将正确的参数传递给您的函数。

    2) 为什么要定义自己的sqrt 而不是使用math.sqrt

    3) 当我运行这段代码时,我得到一个完全不同的错误:ValueError: negative number cannot be raised to a fractional power。这正是您要运行的内容吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-19
      • 2015-04-06
      • 2022-01-02
      • 1970-01-01
      • 2015-11-24
      • 2020-12-26
      • 2018-05-22
      相关资源
      最近更新 更多