2016-11-07 68 views
1

我需要一些帮助,以便学校项目使用此代码。Python - TypeError:无法将复杂的转换为浮点数

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 

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

+1

看起来你的功能设置为接受弧度作为输入,但你通过度... – mgilson

回答

0

1)您正在调用一个名为radToDeg的函数,输入角度为度。确保你传递了正确的参数给你的函数。

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

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

相关问题