# 之前2.14代码计算三角形面积
x1, y1, x2, y2, x3, y3 = eval(input("Enter three points for a triangle: "))
x_one = pow(x1 - x2, 2)
x_two = pow(x2 - x3, 2)
x_three = pow(x3 - x1, 2)
y_one = pow(y1 - y2, 2)
y_two = pow(y2 - y3, 2)
y_three = pow(y3 - y1, 2)
dis_one = pow(x_one + y_one, 0.5)
dis_two = pow(x_two + y_two, 0.5)
dis_three = pow(x_three + y_three, 0.5)
side_num = (dis_one + dis_two + dis_three) / 2
area_num = pow(side_num * (side_num - dis_one) * (side_num - dis_two) * (side_num - dis_three), 0.5)
print("The area of the triangle is {:.2f}".format(area_num))
# 4.19计算三角形三个边的合法性,但我们只要合法性不需要她的周长
a, b, c = 3, 3, 5
list_group = [a, b, c]
list_group.sort(reverse=True)
print(list_group)
print(type(list_group))
if list_group[0] > list_group[1] + list_group[2]:
print("The input is invalid")
else:
print(f"The perimeter is {a + b + c}")
接下来结合两个之前的代码设计这次需要的内容
def area(d1, d2, d3):
side_num = (d1 + d2 + d3) / 2
area_num = pow(side_num * (side_num - d1) * (side_num - d2) * (side_num - d3), 0.5)
return "The area of the triangle is {:.20f}".format(area_num)
def isValid(a, b, c):
list_group = [a, b, c]
list_group.sort(reverse=True)
if list_group[0] > list_group[1] + list_group[2]:
return True
def MyTriangle(a, b, c):
if isValid(a, b, c) is True:
print("The input is invalid")
else:
a_t = area(a, b, c)
print(a_t)
MyTriangle(1, 3, 1)
MyTriangle(1, 1, 1)