如何用例子編寫 Python 程序求球體的體積和表面積?在我們進入 Python 程序尋找球體的體積和表面積之前,讓我們看看定義和公式
球體的 Python 表面積
球體看起來像一個籃球,或者我們可以說是一個圓的三維視圖。如果我們知道球體的半徑,那麼我們可以用公式計算球體的表面積:
球體的表面積= 4πr(其中 r 是球體的半徑)。
根據上面的公式,如果我們知道球體的表面積,那麼我們可以使用以下公式計算球體的半徑:
球面半徑= √sa / 4π(其中 sa 是球面的表面積)。
球體的 Python 體積
球體內部的空間量稱為體積。如果我們知道球體的半徑,那麼我們可以用公式計算球體的體積:
球體的體積= 4πr
尋找球體體積和表面積的 Python 程序
我們將 pi 定義為全局變數,賦值為 3.14。這個 python 程序允許用戶輸入半徑值,然後根據公式計算球體的表面積和體積。
# Python Program to find Volume and Surface Area of Sphere
PI = 3.14
radius = float(input('Please Enter the Radius of a Sphere: '))
sa = 4 * PI * radius * radius
Volume = (4 / 3) * PI * radius * radius * radius
print("\n The Surface area of a Sphere = %.2f" %sa)
print("\n The Volume of a Sphere = %.2f" %Volume)
在這個尋找球體體積和表面積的 Python 程序中,我們輸入了球體半徑= 5
球體的表面積是
表面積= 4πr
表面積= 4 π半徑半徑;
表面積= 4 3.14 5 5
表面積= 314
球體的體積是
體積= 4πr
體積=(4.0/3)π半徑半徑半徑;
體積=(4.0/3) 3.14 5 5 5;
卷= 523.33333
讓我們用表面積來計算球體的半徑:
在上面的 Python 示例中,當半徑= 5 時,我們得到了球體的表面積= 314。讓我們做相反的方法(從表面積計算半徑= 5)
球體半徑= √sa / 4π
球體半徑= √314 / 4 * 3.14
球體半徑= √314 / 12.56
球體半徑= √25
球體半徑= 5
用函數求球體體積和表面積的 Python 程序
這個 python 程序允許用戶輸入半徑值。我們將半徑值傳遞給函數參數,然後它將根據公式計算球體的表面積和體積。
# Python Program to find Volume and Surface Area of Sphere using Functions
import math
def Area_of_Triangle(radius):
sa = 4 * math.pi * radius * radius
Volume = (4 / 3) * math.pi * radius * radius * radius
print("\n The Surface area of a Sphere = %.2f" %sa)
print("\n The Volume of a Sphere = %.2f" %Volume)
Area_of_Triangle(6)
球體輸出的 Python 表面積和體積
The Surface area of a Sphere = 452.39
The Volume of a Sphere = 904.78
>>> Area_of_Triangle(11)
The Surface area of a Sphere = 1520.53
The Volume of a Sphere = 5575.28
>>>
在這個尋找球體體積和表面積的 Python 程序中,首先,我們使用以下語句導入了數學庫。這將允許我們使用數學函數,比如 math.pi
import math
第二步:接下來,我們使用 def 關鍵字用一個參數定義函數。這意味著,用戶將輸入一個球體的半徑。
第三步:根據公式計算球體的表面積和體積
注意:在放置開括弧和閉括弧時請小心,如果放置錯誤,可能會改變整個計算
原創文章,作者:簡單一點,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/130151.html