2012-03-09 136 views
0

我試圖創建一個程序來執行正弦規則,但是我得到上面的錯誤,我檢查了其他問題,但是我無法理解它。Python - Easy - TypeError:無法乘以類型'float'的非int的序列

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") 
+0

'raw_input()'已經返回字符串。你爲什麼在他們周圍有'str()'?你可能想要做'float()'。 – 2012-03-09 22:37:25

回答

3

你可能打算在任何地方調用float()而不是str()。

3

你有

A = str(... 
a = str(... 
B = str(... 

這將它們轉換爲字符串。如果你想在你的方程中乘它們,你需要將它們轉換成浮點數。

在Python中,字符串是序列所以當你

math.sin((PI/180)*A 

你試圖通過一個浮動乘以一個字符串A

0

您的變量AB等是字節串(因此是序列),而PI是浮點數。這是錯誤來自的地方。要將它們視爲數字,您必須將它們從字節字符串轉換爲浮點數,如math.sin(PI/180 * float(A))

相關問題