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("不能构成三角形")
截图: