为什么我不断从此代码得到错误

时间:2020-10-24 06:06:02

标签: python trigonometry

所以我不断收到此错误 打印(float(side /(math.sin(math.radians(float(degree)))))) TypeError:/的不支持的操作数类型:“ str”和“ float”

import math
# for anyone looking at this input opp, 10.7, 65, sin, no
print("This only works for right triangles")
print("opp = opposite   adj = adjacent")

# variables
side1 = input("Input if your trying to find opposite or adjacent: ")
side = input("input length of one side: ")
degree = input("Input angle cant use the right angle: ")
trig_ratio = input("Input either sin cos tan: ")
pos_angle = input("is the right angle below the angle: ")

# sin
if trig_ratio == "sin"\
        and pos_angle == "no"\
        and side1 == "opp":
    print(float(side / (math.sin(math.radians(float(degree))))))

4 个答案:

答案 0 :(得分:1)

问题是您试图将字符串除以浮点数。变量side永远不会转换为浮点数,这就是“ TypeError:/:'str'和'float'所不支持的操作数类型”的意思。

改为使用side = float(input("input length of one side: "))print(float(side) / (math.sin(math.radians(float(degree)))))

答案 1 :(得分:0)

您输入的side变量为str,在进行任何算术运算之前将其转换为float

side = float(input("input length of one side: "))

答案 2 :(得分:0)

输入函数总是返回一个字符串, 尝试将一侧转换为整数或在输入上浮动

side = float(input("input length of one side: "))

答案 3 :(得分:0)

您正在尝试使用字符串值进行除法,即,您需要先将“ side”值转换为浮点数,然后再尝试进行除法。

使用以下代码:

print(float(side)/(math.sin(math.radians(float(degree)))))