Python-Easy-TypeError:can';t将序列乘以类型为'的非整数;浮动';
我试图创建一个程序来执行正弦规则,但我得到了上面的错误,我已经检查了其他问题,但我无法理解它Python-Easy-TypeError:can';t将序列乘以类型为'的非整数;浮动';,python,Python,我试图创建一个程序来执行正弦规则,但我得到了上面的错误,我已经检查了其他问题,但我无法理解它 import math PI = math.pi x = raw_input ("To get length please enter 1, to get angle please enter 2 : ") while x: if x == "1": print ("This is to find a length using the Sine
import math
PI = math.pi
x = raw_input ("To get length please enter 1, to get angle please enter 2 : ")
while x:
if x == "1":
print ("This is to find a length using the Sine Rule")
A= str(raw_input ("what is angle A? "))
a= str(raw_input ("what is length a? "))
B= str(raw_input ("what is angle B? "))
b = (a / math.sin((PI/180)*A)) * (math.sin((PI/180)*B))
print (' your answer is : ', b ,'cm')
raw_input ("press <enter> to end")
if x == "2":
print ("This is to find an angle using the Sine Rule")
A= float(raw_input ("what is angle A? "))
a= raw_input ("what is length a? ")
b= raw_input ("what is length b? ")
B = ( math.sin((PI/180)*A) / a) * b
print (' your answer is : ', B ,'degrees')
raw_input ("press <enter> to end")
导入数学
PI=math.PI
x=原始输入(“要获取长度,请输入1;要获取角度,请输入2:”)
而x:
如果x==“1”:
打印(“这是使用正弦规则查找长度”)
A=str(原始输入(“角度A是什么?”))
a=str(原始输入(“长度a是多少?”)
B=str(原始输入(“角度B是什么?”)
b=(a/math.sin((PI/180)*a))*(math.sin((PI/180)*b))
打印('您的答案是:',b,'cm')
原始输入(“按下结束”)
如果x==“2”:
打印(“这是使用正弦规则查找角度”)
A=浮动(原始输入(“角度A是什么?”))
a=原始输入(“a的长度是多少?”)
b=原始输入(“长度b是多少?”)
B=(数学sin((PI/180)*A)/A)*B
打印('你的答案是:',B,'学位')
原始输入(“按下结束”)
您可能想在任何地方调用float()而不是str()。您可能想在任何地方调用float()而不是str()。您已经
A = str(...
a = str(...
B = str(...
这会将它们转换为字符串。如果要在方程式中乘以它们,需要将它们转换为浮点数
在Python中,字符串是序列,因此
math.sin((PI/180)*A
您正在尝试将字符串a
乘以浮点。您有
A = str(...
a = str(...
B = str(...
这会将它们转换为字符串。如果要在方程式中乘以它们,需要将它们转换为浮点数
在Python中,字符串是序列,因此
math.sin((PI/180)*A
您试图将字符串
a
乘以浮点。您的变量a
,B
等是字节字符串(因此是序列),PI
是浮点。这就是错误的来源。要将它们视为数字,您必须将它们从字节字符串转换为浮点数,如在math.sin(PI/180*float(A))
中,您的变量A
,B
等是字节字符串(因此是序列),PI
是浮点数。这就是错误的来源。要将它们视为数字,您必须将它们从字节字符串转换为浮点数,如math.sin(PI/180*float(A))
raw_input()
已经返回字符串。为什么它们周围有str()
?您可能希望改为执行float()
。raw\u input()
已返回字符串。为什么它们周围有str()
?您可能希望改为执行float()
。