猿问

为什么我总是从这段代码中收到错误

所以我不断收到此错误 print(float(side / (math.sin(math.radians(float( Degree)))))) TypeError: unsupported operand type(s) for /: 'str' and '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))))))


冉冉说
浏览 138回答 4
4回答

ibeautiful

问题是你试图将字符串除以浮点数。该变量side永远不会转换为浮点数,这就是“TypeError: unsupported operand type(s) for /: 'str' and 'float'”所讨论的内容。使用side = float(input("input length of one side: "))或print(float(side) / (math.sin(math.radians(float(degree)))))代替

一只萌萌小番薯

您输入的side变量是,在进行任何算术运算之前str将其转换为。floatside = float(input("input length of one side: "))

跃然一笑

输入函数总是返回一个字符串,尝试在输入时将 side 转换为 int 或 floatside = float(input("input length of one side: "))

开心每一天1111

您正在尝试用字符串值除法,即,您需要先将“side”值转换为浮点数,然后尝试除法。使用下面的代码:print(float(side) / (math.sin(math.radians(float(度)))))
随时随地看视频慕课网APP

相关分类

Python
我要回答