- """
- 计算三角形面积
- 介绍:
- 已知三角形边长分别为x、y、z,可以计算三角形半周长q,然后根据海伦公式计算三角形面积S
- 三角形半周长:q == (x + y + z) / 2
- 三角形面积:S = (q * (q-x) * (q-y) * (q-z)) ** 0.5
- 知识点:
- 1、字符串分割函数split()
- 举一反三:
- 1、如何保留小数点后两位输出面积
- """
-
-
- num = input('请输入三角形的边长:')
-
- # 字符串分割
- one_length = float(num.split()[0])
- two_length = float(num.split()[1])
- three_length = float(num.split()[2])
-
- # 半周长
- half_C = half_circumference = (one_length + two_length + three_length) / 2
-
- # 面积
- area = (half_C * (half_C - one_length) * (half_C - two_length) * (half_C - three_length)) ** 0.5
-
- # 打印
- print(f'三角形的面积为:{area}')
-
-
运行结果:

如需参考另一种方法如下链接:
Python 三角形面积计算_周华2022的博客-CSDN博客
作者:周华
创作日期:2023/9/22