2017-07-16 118 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! –