编写一个 Python 程序,使用底和高通过实际示例查找三角形的面积。
此 Python 程序允许用户输入三角形的底和高。通过使用底和高值,它可以找到三角形的面积。使用底和高查找三角形面积的数学公式:面积 = (底 * 高) / 2。
base = float(input('Please Enter the Base of a Triangle: '))
height = float(input('Please Enter the Height of a Triangle: '))
# calculate the area
area = (base * height) / 2
print("The Area of a Triangle using", base, "and", height, " = ", area)

Python 查找三角形面积的程序(使用底和高)示例 2
这个三角形面积的程序与上面相同。但是,我们使用Python 函数概念将三角形面积程序逻辑分开了。
def area_of_triangle(base, height):
return (base * height) / 2
base = float(input('Please Enter the Base of a Triangle: '))
height = float(input('Please Enter the Height of a Triangle: '))
# calculate the area
area = area_of_triangle(base, height)
print("The Area of a Triangle using", base, "and", height, " = ", area)
使用底和高查找三角形面积的输出。
Please Enter the Base of a Triangle: 35
Please Enter the Height of a Triangle: 85
The Area of a Triangle using 35.0 and 85.0 = 1487.5