如何用例子編寫 Python 程序求圓錐的體積和表面積?在我們進入 Python 程序尋找圓錐體的體積和表面積之前,讓我們看看定義和公式。
圓錐的 Python 表面積
如果我們知道圓錐的半徑和傾斜度,那麼我們使用下面的公式計算圓錐的表面積:
表面積=圓錐的面積+圓的面積
表面積= πrl + πr
其中 r =半徑而
l =傾斜度(從圓錐頂部到圓錐邊緣的邊的長度)
如果我們知道圓錐的半徑和高度,那麼我們就可以用下面的公式計算圓錐的表面積:
表面積= πr +πr √h + r
我們也可以把它寫成:
表面積= πr (r+√h + r)
因為半徑、高度和傾斜使形狀成為直角三角形。所以,利用勾股定理:
l = h + r
l = √h + r
圓錐的 Python 體積
圓錐體內部的空間量稱為體積。如果我們知道圓錐體的半徑和高度,那麼我們可以使用公式計算體積:
體積= 1/3 πr h(其中 h=圓錐體的高度)
圓錐的側面面積=πR1
尋找圓錐體積和表面積的 Python 程序
這個 python 程序允許用戶輸入圓錐體的半徑和高度值。使用這些值,它將根據公式計算圓錐的表面積、體積、邊長(傾斜)和側表面積。
# Python Program to find Volume and Surface Area of a Cone
import math
radius = float(input('Please Enter the Radius of a Cone: '))
height = float(input('Please Enter the Height of a Cone: '))
# Calculate Length of a Slide (Slant)
l = math.sqrt(radius * radius + height * height)
# Calculate the Surface Area
SA = math.pi * radius * (radius + l)
# Calculate the Volume
Volume = (1.0/3) * math.pi * radius * radius * height
# Calculate the Lateral Surface Area
LSA = math.pi * radius * l
print("\n Length of a Side (Slant)of a Cone = %.2f" %l)
print(" The Surface Area of a Cone = %.2f " %SA)
print(" The Volume of a Cone = %.2f" %Volume);
print(" The Lateral Surface Area of a Cone = %.2f " %LSA)
在這個尋找圓錐體積和表面積的 Python 程序中,首先,我們使用下面的語句導入了數學庫。這將允許我們使用數學函數,如 math.pi 和 math.sqrt。如果你沒有包括這一行,那麼 math.pi 將通過一個錯誤。
import math
在下方,Python 語句將要求用戶輸入半徑和高度值,並將用戶輸入值分配給相關變量。例如第一個值將分配給半徑,第二個值分配給高度
radius = float(input('Please Enter the Radius of a Cone: '))
height = float(input('Please Enter the Height of a Cone: '))
接下來,我們將使用它們各自的公式計算圓錐體的體積、表面積、側面表面積和邊長(斜面):
# Calculate Length of a Slide (Slant)
l = math.sqrt(radius * radius + height * height)
# Calculate the Surface Area
SA = math.pi * radius * (radius + l)
# Calculate the Volume
Volume = (1.0/3) * math.pi * radius * radius * height
# Calculate the Lateral Surface Area
LSA = math.pi * radius * l
以下打印語句將幫助我們打印立方體的體積和表面積
print("\n Length of a Side (Slant)of a Cone = %.2f" %l)
print(" The Surface Area of a Cone = %.2f " %SA)
print(" The Volume of a Cone = %.2f" %Volume);
print(" The Lateral Surface Area of a Cone = %.2f " %LSA)
對於這個尋找圓錐體積和表面積的 Python 程序,我們已經輸入了圓錐半徑= 5 和高度= 12
根據勾股定理,我們可以計算出斜面(邊長):
l = h+r
l =√h+r
l =√12+5
=>√144+25
l =√169
l = 13
圓錐體的表面積是
圓錐體的表面積= πr +πrl
圓錐體的表面積= πr (r + l)
它的意思是,圓錐體的表面積=數學π半徑(半徑+ l)
圓錐體的表面積= 3.14 5 (5+13)=>3.14 5 18
圓錐體的表面積= 282.6
圓錐的體積是
圓錐的體積= 1/3 πr h
它的意思是,圓錐的體積= (1.0/3) 數學π半徑半徑高度
圓錐的體積=(1.0/3) 3.14 5 5 12;
圓錐體的體積= 314
圓錐體的側表面面積為
側表面面積= πrl
這意味着,側表面面積=數學π半徑 l
側表面面積= 3.14 5 13
側表面面積= 204.1
讓我們使用半徑而不使用斜面(標準公式)來計算圓錐的半徑:
圓錐的表面積= πr +πr √h + r
圓錐的表面積= πr (r + √h + r )
意思是,表面積=數學π半徑(半徑+數學 sqrt((高度高度)+(半徑半徑))
圓錐體的表面積= 3.14 5 ( 5 + √12 + 5 )
圓錐體的表面積= 3.14 5 (5+√169)
=>3.14 5 (5+13)
圓錐體的表面積= 3.14 5 18
表面積
用函數求圓錐體積和表面積的 Python 程序
這個 python 程序允許用戶輸入圓錐體的半徑和高度值。我們將半徑和高度值傳遞給函數參數,然後它將根據公式計算圓錐體的表面積和體積。
# Python Program to find Volume and Surface Area of a Cone using functions
import math
def Vo_Sa_Cone(radius, height):
# Calculate Length of a Slide (Slant)
l = math.sqrt(radius * radius + height * height)
# Calculate the Surface Area
SA = math.pi * radius * (radius + l)
# Calculate the Volume
Volume = (1.0/3) * math.pi * radius * radius * height
# Calculate the Lateral Surface Area
LSA = math.pi * radius * l
print("\n Length of a Side (Slant)of a Cone = %.2f" %l)
print(" The Surface Area of a Cone = %.2f " %SA)
print(" The Volume of a Cone = %.2f" %Volume)
print(" The Lateral Surface Area of a Cone = %.2f " %LSA)
Vo_Sa_Cone(6,10)
首先,我們使用 def 關鍵字定義了帶有兩個參數的函數。這意味着,用戶將輸入圓錐的半徑和高度。使用這些值,上面的函數將計算球體的表面積和體積,正如我們在第一個示例 中所解釋的那樣
Length of a Side (Slant)of a Cone = 11.66
The Surface Area of a Cone = 332.92
The Volume of a Cone = 376.99
The Lateral Surface Area of a Cone = 219.82
>>> Vo_Sa_Cone(5,12)
Length of a Side (Slant)of a Cone = 13.00
The Surface Area of a Cone = 282.74
The Volume of a Cone = 314.16
The Lateral Surface Area of a Cone = 204.20
>>>
注意:我們可以用中的參數調用函數。或者我們可以從 python shell 中調用它。請不要忘記函數參數
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/235842.html