已知三角形的三边长a,b,c,利用海伦公式求三角形面积

if语句

代码:

a = float(input("输入边长a:"))
b = float(input("输入边长b:"))
c = float(input("输入边长c:"))
if a+b > c and a+c > b and b+c > a:
    s = (a+b+c)/2
    area = (s*(s-a)*(s-b)*(s-c))**0.5
    print("三角形的面积为:", area)
else:
    print("不能构成三角形")

截图:

while语句

代码:

a = float(input("输入边长a:"))
b = float(input("输入边长b:"))
c = float(input("输入边长c:"))
while not (a+b > c and a+c > b and b+c > a):
    print("不能构成三角形,请重新输入")
    a = float(input("输入边长a:"))
    b = float(input("输入边长b:"))
    c = float(input("输入边长c:"))
s = (a+b+c)/2
area = (s*(s-a)*(s-b)*(s-c))**0.5
print("三角形的面积为:", area)

 截图:

 函数调用

代码:

def hailun(a, b, c):
    s = (a+b+c)/2
    area = (s*(s-a)*(s-b)*(s-c))**0.5
    return area
a = float(input("输入边长a:"))
b = float(input("输入边长b:"))
c = float(input("输入边长c:"))
while(a+b>c and a+c>b and b+c>a):
    area = hailun(a, b, c)
    print("三角形的面积为", area)
    break
else:
    print("不能构成三角形")

截图:

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值