首页 > 编程知识 正文

Python 程序:计算圆锥体积和表面积

时间:2023-11-25 09:12:54 阅读:309022 作者:PZEB

如何用例子编写 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 中调用它。请不要忘记函数参数

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。