为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

冉冉说 2023-12-29 17:12:17
所以我不断收到此错误 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, noprint("This only works for right triangles")print("opp = opposite   adj = adjacent")# variablesside1 = 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: ")# sinif trig_ratio == "sin"\        and pos_angle == "no"\        and side1 == "opp":    print(float(side / (math.sin(math.radians(float(degree))))))
查看完整描述

4 回答

?
ibeautiful

TA贡献1993条经验 获得超5个赞

问题是你试图将字符串除以浮点数。该变量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)))))代替


查看完整回答
反对 回复 2023-12-29
?
一只萌萌小番薯

TA贡献1795条经验 获得超7个赞

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

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


查看完整回答
反对 回复 2023-12-29
?
跃然一笑

TA贡献1826条经验 获得超6个赞

输入函数总是返回一个字符串,尝试在输入时将 side 转换为 int 或 float

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


查看完整回答
反对 回复 2023-12-29
?
开心每一天1111

TA贡献1836条经验 获得超13个赞

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

使用下面的代码:

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


查看完整回答
反对 回复 2023-12-29
  • 4 回答
  • 0 关注
  • 72 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信