简体   繁体   English

TypeError:需要浮点数-Python

[英]TypeError: a float is required - Python

I am currently creating a trigonometric calculator via Python 3.x. 我目前正在通过Python 3.x创建一个三角计算器。 In one of my functions, I created a value for an unknown angle of a right triangle 'angle_b' which I defined by assigning it the function 'ANGLE_B'. 在我的函数之一中,我为直角三角形“ angle_b”的未知角度创建了一个值,该值是通过为其分配函数“ ANGLE_B”来定义的。 Here's the code tree for reference: 这是代码树供参考:

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

I have tried numerous combinations of converting ang in the ANGLE_B() block into a floating point number such as ang = float(ang) yet i've had no luck. 我曾尝试变换的众多组合angANGLE_B()块分成一个浮点数如ang = float(ang)然而,我没有运气。 Can anyone help? 有人可以帮忙吗? I keep getting TypeError: a float is required when I run it in CMD. 我不断收到TypeError: a float is required在CMD中运行它时TypeError: a float is required

You're passing string variables to math.sin and math.asin, which is causing the type error. 您正在将字符串变量传递给math.sin和math.asin,这会导致类型错误。 You can fix by converting to float: 您可以通过转换为float来解决:

ang = format(math.asin(float(side_b)* (math.sin(float(angle_a))/float(side_a))),'.3f')

You could also just store all these variables as floats to begin with. 您也可以将所有这些变量存储为浮点数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM