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

目前創(chuàng)新互聯(lián)建站已為上千的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬空間、網(wǎng)站運(yùn)營、企業(yè)網(wǎng)站設(shè)計、鎮(zhèn)江網(wǎng)站維護(hù)等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
如何用例子編寫 Python 程序求長方體的體積和表面積?在我們進(jìn)入 Python 程序?qū)ふ议L方體的體積和表面積之前,讓我們看看長方體的頂面和底面表面積、側(cè)面表面積后面的定義和公式
Python 長方體
長方體是由 6 個矩形組成的三維物體。所有相對的面(即頂部和底部)都是相等的。
長方體的表面積
長方體的總表面積是長方體中所有 6 個矩形面積的總和。如果我們知道長方體的長度、寬度和高度,那么我們可以使用以下公式計算總表面積:
頂面和底面面積= lw+lw = 2w
前后表面面積= lh + lh = 2lh
兩側(cè)面積= wh + wh = 2wh
長方體的總表面積是所有 6 個面的總和。因此,我們必須將所有這些面積相加,以計算最終的表面積
長方體的總表面積= 2lw + 2lh + 2wh
相等:總表面積= 2 (lw + lh +wh)
長方體的體積
長方體內(nèi)部的空間量叫做體積。如果我們知道長方體的長度、寬度和高度,那么我們可以用下面的公式計算體積:
長方體的體積=長寬高
長方體的體積=磅
長方體的側(cè)面面積= 2h (l + w)
Python 程序求長方體的體積和表面積
這個 Python 程序允許用戶輸入長方體的長度、寬度和高度。利用這些值,編譯器將根據(jù)公式計算長方體的表面積、體積和側(cè)面面積。
# Python Program to find Volume and Surface Area of Cuboid
length = float(input('Please Enter the Length of a Cuboid: '))
width = float(input('Please Enter the Width of a Cuboid: '))
height = float(input('Please Enter the Height of a Cuboid: '))
# Calculate the Surface Area
SA = 2 * (length * width + length * height + width * height)
# Calculate the Volume
Volume = length * width * height
# Calculate the Lateral Surface Area
LSA = 2 * height * (length + width)
print("\n The Surface Area of a Cuboid = %.2f " %SA)
print(" The Volume of a Cuboid = %.2f" %Volume);
print(" The Lateral Surface Area of a Cuboid = %.2f " %LSA)下面的語句將要求用戶輸入長度、寬度和高度值,并將用戶輸入值分配給相應(yīng)的變量。例如第一個值將分配給長度,第二個值分配給寬度,第三個值分配給高度
length = float(input('Please Enter the Length of a Cuboid: '))
width = float(input('Please Enter the Width of a Cuboid: '))
height = float(input('Please Enter the Height of a Cuboid: '))接下來,我們將使用長方體的體積、表面積和側(cè)表面積各自的公式來計算它們:
# Calculate the Surface Area
SA = 2 * (length * width + length * height + width * height)
# Calculate the Volume
Volume = length * width * height
# Calculate the Lateral Surface Area
LSA = 2 * height * (length + width)遵循 Python 打印語句將幫助我們打印長方體的體積和表面積
print("\n The Surface Area of a Cuboid = %.2f " %SA)
print(" The Volume of a Cuboid = %.2f" %Volume);
print(" The Lateral Surface Area of a Cuboid = %.2f " %LSA)
在上面的 Python 程序中查找長方體的體積和表面積的例子中,我們插入了長度= 8,寬度= 5 和高度= 6 的值
給定度量的長方體體積為:
長方體的體積= lbh = l w h 長方體的體積=長寬高 長方體的體積= 8 5 6 長方體的體積= 240
長方體的體積是 240
對于給定的度量,長方體的總表面積為:
長方體總表面積= 2lw + 2lh + 2wh 長方體總表面積= 2 (lw + lh +wh) 長方體總表面積= 2(長寬+長高+寬高) 長方體總表面積= 2 (8 5)+(8 6)+(5 6)) 長方體總表面積= 2 (40 + 48 + 30) 長方體總表面積= 2 118 長方體總表面積= 236
長方體的總表面積是 236
對于給定的測量,長方體的側(cè)面面積為:
長方體側(cè)面面積= 2lh + 2wh 長方體側(cè)面面積= 2h (l + w) 長方體側(cè)面面積= 2 高(長+寬) 長方體側(cè)面面積= 2 6 (8 + 5) 長方體側(cè)面面積= 2 6 (13 ) 長方體側(cè)面面積= 156T8】
長方體的側(cè)面面積是 156
用函數(shù)求長方體體積和表面積的 Python 程序
這個 python 程序允許用戶輸入長度、寬度和高度值。我們將這些值傳遞給函數(shù)參數(shù),然后它將根據(jù)公式計算長方體的表面積和體積。
# Python Program to find Volume and Surface Area of a Cuboid using Functions
def Vo_Sa_Cuboid(length, width, height):
# Calculate the Surface Area
SA = 2 * (length * width + length * height + width * height)
# Calculate the Volume
Volume = length * width * height
# Calculate the Lateral Surface Area
LSA = 2 * height * (length + width)
print("\n The Surface Area of a Cuboid = %.2f " %SA)
print(" The Volume of a Cuboid = %.2f" %Volume)
print(" The Lateral Surface Area of a Cuboid = %.2f " %LSA)
Vo_Sa_Cuboid(9, 4, 6)我們使用 def 關(guān)鍵字用三個參數(shù)定義了這個函數(shù)。這意味著,用戶將輸入長方體的長度、寬度和高度值。這個 Python 程序?qū)⒂嬎汩L方體的表面積和體積,就像我們在第一個例子 中解釋的那樣
The Surface Area of a Cuboid = 228.00
The Volume of a Cuboid = 216.00
The Lateral Surface Area of a Cuboid = 156.00
>>> Vo_Sa_Cuboid(8, 5, 6)
The Surface Area of a Cuboid = 236.00
The Volume of a Cuboid = 240.00
The Lateral Surface Area of a Cuboid = 156.00
>>> 注意:我們可以用中的參數(shù)調(diào)用函數(shù)?;蛘呶覀兛梢詮?python shell 中調(diào)用它。請不要忘記函數(shù)參數(shù)
分享題目:Python程序:求長方體的體積和表面積
標(biāo)題來源:http://m.5511xx.com/article/coshgop.html


咨詢
建站咨詢
