类型错误:需要浮点数 - Python

Ole*_*kin 2 python floating-point typeerror required

我目前正在通过 Python 3.x 创建一个三角计算器。在我的一个函数中,我为直角三角形“angle_b”的未知角度创建了一个值,我通过为其分配函数“ANGLE_B”来定义该值。这是供参考的代码树:

def create():
    global side_a
    side_a = format(random.uniform(1,100),'.0f')
    global side_b
    side_b = format(random.uniform(1,100),'.0f')
    global angle_a
    angle_a = format(random.uniform(1,180),',.3f')
    global angle_b
    angle_b = ANGLE_B()

def ANGLE_B():
    ang = format(math.asin(side_b*(math.sin(angle_a)/side_a)),'.3f')
    return ang
Run Code Online (Sandbox Code Playgroud)

我已经尝试了多种将块转换为浮点数的组合angANGLE_B()ang = float(ang)我没有运气。有人可以帮忙吗?TypeError: a float is required当我在CMD中运行它时,我不断收到。

sam*_*gak 5

您将字符串变量传递给 math.sin 和 math.asin,这导致了类型错误。您可以通过转换为浮点来修复:

ang = format(math.asin(float(side_b)* (math.sin(float(angle_a))/float(side_a))),'.3f')
Run Code Online (Sandbox Code Playgroud)

您也可以一开始就将所有这些变量存储为浮点数。