海伦公式
根据三角形的三条边长度a,b,c,计算三角形的面积S
p = (a + b + c)/2
S^2 = p * (p - a) * (p - b) * (p - c)
海伦公式的Python实现
#输入三条变边长分别赋值给a,b,c
a,b,c = eval(input("give three number separated by comma:"))
#判断给定的三条边是否能组成一个三角形
while not (a+b>c and a+c>b and b+c>a):
print('pls give 3 nums once again!')
a,b,c = eval(input("give three number separated by comma:"))
p = (a + b + c)/2
area = (p * (p - a) * (p - b) * (p - c)) ** 0.5
print('三角形的面积为:{0:9.3f}'.format(area))
网友评论