用一個實際例子編寫 Python 程序,用底和高來求三角形的面積。
這個 Python 程序允許用戶輸入三角形的底部和高度。通過使用底部和高度值,它可以找到三角形的面積。用底和高求三角形面積的數學公式:面積=(底*高)/ 2。
# Python Program to find Area of a Triangle using base and height
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 程序對於三角形的面積和上面一樣。然而,我們使用 Python 函數概念分離了三角形的面積程序邏輯。
# Python Program to find Area of a Triangle using base and height
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)
使用底部和高度輸出的三角形的 Python 面積
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
原創文章,作者:E3C72,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/126498.html