自定义两个函数,输入长宽高和密度求质量的程序
时间: 2024-03-09 08:45:44 浏览: 24
好的,以下是两个函数的代码:
```python
# 第一个函数:根据长宽高和密度计算质量
def calculate_mass(length, width, height, density):
volume = length * width * height # 计算体积
mass = volume * density # 计算质量
return mass # 返回质量
# 第二个函数:在第一个函数基础上计算表面积
def calculate_surface_area(length, width, height, density):
mass = calculate_mass(length, width, height, density) # 调用第一个函数计算质量
surface_area = 2 * (length * width + width * height + height * length) # 计算表面积
return mass, surface_area # 返回质量和表面积
```
使用示例:
```python
# 调用第一个函数计算质量
mass = calculate_mass(10, 5, 2, 1.5)
print("质量为:", mass)
# 调用第二个函数计算质量和表面积
mass, surface_area = calculate_surface_area(10, 5, 2, 1.5)
print("质量为:", mass)
print("表面积为:", surface_area)
```
输出结果:
```
质量为: 150.0
质量为: 150.0
表面积为: 220.0
```
注:以上代码中的计算公式是基于常见的长方体模型,如果您需要计算其他形状的物体,需要根据不同的公式进行计算。