Oleg Silkin
Oleg Silkin

Reputation: 528

TypeError: a float is required - Python

I am currently creating a trigonometric calculator via 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'. 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. Can anyone help? I keep getting TypeError: a float is required when I run it in CMD.

Upvotes: 2

Views: 14851

Answers (1)

samgak
samgak

Reputation: 24417

You're passing string variables to math.sin and math.asin, which is causing the type error. You can fix by converting to 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.

Upvotes: 5

Related Questions