日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python 程序:計(jì)算梯形面積

創(chuàng)新互聯(lián)python教程:

如何用例子編寫 Python 程序求梯形面積和梯形中值?在我們進(jìn)入實(shí)際的 Python 程序?qū)ふ姨菪蚊娣e的例子之前,讓我們看看定義和公式

梯形的 Python 面積

如果我們知道高度和兩個(gè)底長,那么我們可以用下面的公式計(jì)算梯形的面積:

面積= (a+b)/2 * h

其中 a 和 b 是兩個(gè)底,h 是梯形的高度。我們可以用下面的公式計(jì)算梯形的中值:

中位數(shù)= (a+b) / 2。

如果我們知道中間值和高度,那么我們可以計(jì)算梯形的面積為:中間值*高度

尋找梯形面積的 Python 程序

這個(gè) python 程序允許用戶輸入梯形的兩邊和高度。使用這些值,這個(gè) Python 程序?qū)⒂?jì)算梯形的面積和梯形的中值。

# Python Program to find Area of a Trapezoid

base1 = float(input('Please Enter the First Base of a Trapezoid: '))
base2 = float(input('Please Enter the Second Base of a Trapezoid: '))
height = float(input('Please Enter the Height of a Trapezoid: '))

# calculate the area
Area = 0.5 * (base1 + base2) * height

# calculate the Median
Median = 0.5 * (base1+ base2)

print("\n Area of a Trapezium = %.2f " %Area)
print(" Median of a Trapezium = %.2f " %Median)

以下語句將要求用戶輸入 base1、base2 和 height 值,并將用戶輸入值分配給相關(guān)變量。如第一個(gè)值將賦給 base1,第二個(gè)值賦給 base2,第三個(gè)值賦給高度

base1 = float(input('Please Enter the First Base of a Trapezoid: '))
base2 = float(input('Please Enter the Second Base of a Trapezoid: '))
height = float(input('Please Enter the Height of a Trapezoid: '))

接下來,我們使用它們各自的公式計(jì)算梯形的中值和面積:

# calculate the area
Area = 0.5 * (base1 + base2) * height

# calculate the Median
Median = 0.5 * (base1+ base2)

打印語句將幫助我們打印梯形的中值和面積

print("\n Area of a Trapezium = %.2f " %Area)
print(" Median of a Trapezium = %.2f " %Median)

讓我們看看程序的輸出

Please Enter the First Base of a Trapezoid: 6
Please Enter the Second Base of a Trapezoid: 9
Please Enter the Height of a Trapezoid: 4

 Area of a Trapezium = 30.00 
 Median of a Trapezium = 7.50

從上面的 Python 截圖可以觀察到,我們輸入的值是 base1 = 6,base2 = 9,height = 4

梯形的面積= 0.5 (基底 1 +基底 2)高度; 梯形的面積= 0.5 (6+9) 4; 梯形的面積= 0.5 15 4; 梯形面積= 30

梯形的中值= 0.5 (base 1+base 2); 梯形的中值= 0.5 (6 + 9) 梯形的中值= 0.5 * 15 梯形的中值= 7.5

用函數(shù)求梯形面積的 Python 程序

這個(gè) python 程序允許用戶輸入梯形的基底 1、基底 2 和高度。我們將把這些值傳遞給函數(shù)參數(shù)來計(jì)算梯形的面積。

# Python Program to find Area of a Trapezoid using Functions

def Area_of_a_Trapezoid (base1, base2, height):
    # calculate the area
    Area = 0.5 * (base1 + base2) * height

    # calculate the Median
    Median = 0.5 * (base1+ base2)

    print("\n Area of a Trapezium = %.2f " %Area)
    print(" Median of a Trapezium = %.2f " %Median)

Area_of_a_Trapezoid (9, 6, 4)

在這個(gè)尋找梯形面積的 Python 程序示例中,首先,我們使用 def 關(guān)鍵字定義了具有三個(gè)參數(shù)的函數(shù)。這意味著,用戶將輸入梯形的底 1、底 2 和高度。接下來,我們計(jì)算梯形的中值和面積,正如我們在第一個(gè)例子中所描述的。

注意:我們可以用中的參數(shù)調(diào)用函數(shù)?;蛘呶覀兛梢詮?python shell 中調(diào)用它。請不要忘記函數(shù)參數(shù)


分享標(biāo)題:Python 程序:計(jì)算梯形面積
網(wǎng)頁鏈接:http://www.dlmjj.cn/article/dhgpooc.html