如何用例子編寫 Python 程序求長方體的體積和表面積?在我們進入 Python 程序尋找長方體的體積和表面積之前,讓我們看看長方體的頂面和底面表面積、側面表面積後面的定義和公式
Python 長方體
長方體是由 6 個矩形組成的三維物體。所有相對的面(即頂部和底部)都是相等的。
長方體的表面積
長方體的總表面積是長方體中所有 6 個矩形面積的總和。如果我們知道長方體的長度、寬度和高度,那麼我們可以使用以下公式計算總表面積:
頂面和底面面積= lw+lw = 2w
前後表面面積= lh + lh = 2lh
兩側面積= wh + wh = 2wh
長方體的總表面積是所有 6 個面的總和。因此,我們必須將所有這些面積相加,以計算最終的表面積
長方體的總表面積= 2lw + 2lh + 2wh
相等:總表面積= 2 (lw + lh +wh)
長方體的體積
長方體內部的空間量叫做體積。如果我們知道長方體的長度、寬度和高度,那麼我們可以用下面的公式計算體積:
長方體的體積=長寬高
長方體的體積=磅
長方體的側面面積= 2h (l + w)
Python 程序求長方體的體積和表面積
這個 Python 程序允許用戶輸入長方體的長度、寬度和高度。利用這些值,編譯器將根據公式計算長方體的表面積、體積和側面面積。
# 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)
下面的語句將要求用戶輸入長度、寬度和高度值,並將用戶輸入值分配給相應的變數。例如第一個值將分配給長度,第二個值分配給寬度,第三個值分配給高度
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)
遵循 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
對於給定的測量,長方體的側面面積為:
長方體側面面積= 2lh + 2wh
長方體側面面積= 2h (l + w)
長方體側面面積= 2 高(長+寬)
長方體側面面積= 2 6 (8 + 5)
長方體側面面積= 2 6 (13 )
長方體側面面積= 156T8】
長方體的側面面積是 156
用函數求長方體體積和表面積的 Python 程序
這個 python 程序允許用戶輸入長度、寬度和高度值。我們將這些值傳遞給函數參數,然後它將根據公式計算長方體的表面積和體積。
# 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 關鍵字用三個參數定義了這個函數。這意味著,用戶將輸入長方體的長度、寬度和高度值。這個 Python 程序將計算長方體的表面積和體積,就像我們在第一個例子 中解釋的那樣
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
>>>
注意:我們可以用中的參數調用函數。或者我們可以從 python shell 中調用它。請不要忘記函數參數
原創文章,作者:OE232,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/127520.html