2017-07-16 69 views
0

我收到以下錯誤,似乎無法弄清楚如何解決。遵循邏輯,我將3個函數和所有3個返回值作爲float調用,然後對存儲的返回值執行一些數學運算並將其打印爲float。那麼哪裏出了問題?餘爲A側輸入4和5側B.返回函數值爲float Python 3.6.1

錯誤消息:

輸入側A的長度:4.0 輸入側B的長度:5.0

Traceback (most recent call last): 
    File "python", line 26, in <module> 
    File "python", line 9, in main 
    File "python", line 24, in calculateHypotenuse 
TypeError: unsupported operand type(s) for ^: 'float' and 'float' 

import math 

def main(): 
    #Call get length functions to get lengths. 
    lengthAce = getLengthA() 
    lengthBee = getLengthB() 

    #Calculate the length of the hypotenuse 
    lengthHypotenuse = calculateHypotenuse(float(lengthAce),float(lengthBee)) 

    #Display length of C (hypotenuse) 
    print() 
    print("The length of side C 'the hypotenuse' is {}".format(lengthHypotenuse)) 

#The getLengthA function prompts for and returns length of side A 
def getLengthA(): 
    return float(input("Enter the length of side A: ")) 

#The getLengthA function prompts for and returns length of side B 
def getLengthB(): 
    return float(input("Enter the length of side B: ")) 

def calculateHypotenuse(a,b): 
    return math.sqrt(a^2 + b^2) 

main() 

print() 
print('End of program!') 
+2

如果你正在嘗試使用power operator,你需要使用'**'來代替。在Python中'''具有完全不同的含義。這是按位XOR運算符。 –

回答

1

在Python ^bitwise XOR operator,不是電源操作:

^運算得到的按位異或它的參數(異或),必須是intege

您需要使用**代替,這冪運算符:

def calculateHypotenuse(a,b): 
    return math.sqrt(a**2 + b**2) 
+0

謝謝。那樣做了。 :) – Cornel

+0

很高興幫助@Cornel! –