如何用例子编写 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。 对于给定的度量,长方体的总表面积为: - 长方体总表面积 = 2(lw + lh + wh)
长方体总表面积 = 2 × (8 × 5 + 8 × 6 + 5 × 6)
长方体总表面积 = 2 × (40 + 48 + 30)
长方体总表面积 = 2 × 118
长方体总表面积 = 236 长方体的总表面积是 236。 对于给定的测量,长方体的侧面面积为: - 长方体侧面面积 = 2lh + 2wh
长方体侧面面积 = 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
>>>
注意: 我们可以用参数调用函数。或者我们可以从 Python shell 中调用它。请不要忘记函数参数。