如何编写 Python 程序来计算长方体的体积和表面积,并附带示例。在开始学习 Python 程序计算长方体的体积和表面积之前,让我们先了解一下长方体顶部和底部表面积、侧面积的定义和公式。
Python 长方体
长方体是由 6 个矩形组成的 3D 对象。所有相对的面(即顶部和底部)都是相等的。
长方体的表面积
长方体的总表面积是长方体中所有 6 个矩形面积的总和。如果我们知道长方体的长度、宽度和高度,那么我们可以使用以下公式计算总表面积:
顶部和底部表面积 = lw + lw = 2lw
前面和后面表面积 = lh + lh = 2lh
两侧面积 = wh + wh = 2wh
长方体的总表面积是所有 6 个面的总和。因此,为了计算最终的表面积,我们需要将所有这些面积相加。
长方体的总表面积 = 2lw + 2lh + 2wh
即:总表面积 = 2 (lw + lh +wh)
长方体的体积
长方体内部的空间量称为体积。如果我们知道长方体的长度、宽度和高度,那么我们可以使用以下公式计算体积:
长方体的体积 = 长度 * 宽度 * 高度
长方体的体积 = lbh
长方体的侧面积 = 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 )
长方体的侧面积 = 156
长方体的侧面积为 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
>>>
注意:我们可以直接在 .py 文件中调用带参数的函数,也可以从 Python shell 调用它。请不要忘记函数参数。