Python举例: 输入三角形边长,判断所生成三角形和面积

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import math
a,b,c = input("Input the length of sides separated by comma:")
if a + b > c and a + c > b and b + c > a:
p = (a + b + c)/2.0
temp = p * (p - a) * (p - b) * (p - c)
area = math.sqrt(temp)
if a == b or b == a or c == a:
if a == b == c:
res = "equilateral triangle"
elif math.fabs(a**2 + b**2 - c**2) < 1e-6 or math.fabs(b**2 + c**2 - a**2) < 1e-6 or math.fabs(c**2 + a**2 - b**2) < 1e-6:
res = "isosceles triangle"
elif math.fabs(a**2 + b **2 - c**2) < 1e-6 or (b**2 + c **2 - a**2) < 1e-6 or (c**2 + a **2 - b**2) < 1e-6:
res = "rightangled triangle"
else:
res = "Common triangle"
else:
res = "Not triangle"
if res != "Not triangle":
print("Area for this triangle is", area)
print("It is a", res)
else:
print("Not a triangle")