
python
import math
def calculate_cylinder_volume(radius, height):
pi = math.pi
volume = pi * radius ** 2 * height
return volume
# 假设半径和高度已经输入
radius = 5
height = 10
# 调用函数计算体积
cylinder_volume = calculate_cylinder_volume(radius, height)
print("给定半径为", radius, "高度为", height, "的圆柱体体积为", cylinder_volume)
在这个代码中,我们首先导入了math库,以便使用pi常量。然后定义了一个calculate_cylinder_volume函数,它接收半径和高度作为参数,并使用pi和半径的平方乘以高度来计算体积。最后,我们设置了一些示例值并调用函数来计算体积。
请注意,这个代码假设半径和高度是正数。在实际应用中,你可能需要检查输入以确保它们是有效的。
更多文章请关注《万象专栏》
转载请注明出处:https://www.wanxiangsucai.com/read/cv183750