所以我不斷收到此錯誤 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條經驗 獲得超6個贊
問題是你試圖將字符串除以浮點數。該變量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)))))
代替

一只萌萌小番薯
TA貢獻1795條經驗 獲得超7個贊
您輸入的side
變量是,在進行任何算術運算之前str
將其轉換為。float
side = float(input("input length of one side: "))

躍然一笑
TA貢獻1826條經驗 獲得超6個贊
輸入函數總是返回一個字符串,嘗試在輸入時將 side 轉換為 int 或 float
side = float(input("input length of one side: "))

開心每一天1111
TA貢獻1836條經驗 獲得超13個贊
您正在嘗試用字符串值除法,即,您需要先將“side”值轉換為浮點數,然后嘗試除法。
使用下面的代碼:
print(float(side) / (math.sin(math.radians(float(度)))))
添加回答
舉報
0/150
提交
取消