Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';在Python上无法将复杂转换为浮点错误_Python_Python 3.x_Area_Complex Numbers - Fatal编程技术网

Can';在Python上无法将复杂转换为浮点错误

Can';在Python上无法将复杂转换为浮点错误,python,python-3.x,area,complex-numbers,Python,Python 3.x,Area,Complex Numbers,我写了一个计算三角形面积的代码 a = float(input("Enter the first side of triangle: \n")) b = float(input("Enter the second side of triangle: \n")) c = float(input("Enter the thrid side of triangle: \n")) s = (a+b+c) / 2 area = (s*(s-a) * (s-b) * (s-c)) ** 0.5 pri

我写了一个计算三角形面积的代码

a = float(input("Enter the first side of triangle: \n"))
b = float(input("Enter the second side of triangle: \n"))
c = float(input("Enter the thrid side of triangle: \n"))

s = (a+b+c) / 2

area = (s*(s-a) * (s-b) * (s-c)) ** 0.5

print("The area of triangle is %0.3f" %area)
我使用的输入值是:2545565544

我收到的错误是:

print("The area of triangle is %0.3f" %area)
TypeError: can't convert complex to float

有人能帮我解决这个问题吗?当我输入小的数字时,我的代码工作得很好,比如(5,6,7)。使用Pycharm作为我的IDE。

由于输入边不形成三角形,代码无法工作-
25+4556<5544
。因此,术语
s-c
为负,因此计算平方根返回复数

为确保有有效的方面,请在获取a、b、c的值后添加断言/验证,以检查:

assert a+b+c > 2*max(a, b, c)
这基本上确保了两个较小的边的总和大于最大的边


另一方面,您还可以验证您的各方面都是积极的:

assert all(x>0 for x in (a, b, c))

2545565544
不是有效的三角形。两条较短的边不够长。这就像试着做一个三角形,其中一边的长度为1,第二边的长度为3,第三边的长度为1000。显然不行。你能画出这样一个三角形吗?@ParasKumar:检查我的答案。如果有帮助的话,:)@OliverCharlesworth给我一些4D绘图纸……谢谢你的帮助。