写一个简单的工程计价程序
时间: 2023-11-29 21:03:53 浏览: 44
以下是一个简单的工程计价程序,可以根据输入的材料和工时数量,计算出总费用:
```python
material_costs = {
'cement': 50, # 水泥单价为50元/吨
'steel': 5000, # 钢材单价为5000元/吨
'sand': 30 # 沙子单价为30元/吨
}
def calculate_cost(materials, work_hours):
total_material_cost = sum([material_costs[material] for material in materials])
total_work_cost = 50 * work_hours # 工时费用为每小时50元
total_cost = total_material_cost + total_work_cost
return total_cost
materials = ['cement', 'steel', 'sand']
work_hours = 80
total_cost = calculate_cost(materials, work_hours)
print('Total cost:', total_cost, '元')
```
在这个程序中,我们首先定义了一个材料价格字典 `material_costs`,其中包含了各种材料的单价。然后我们定义了一个 `calculate_cost` 函数,它接受两个参数:材料列表和工时数量。这个函数首先计算出材料的总费用,然后乘以每小时50元的工时费用,最后将两者相加得到总费用,并返回该值。
在主程序中,我们定义了一个材料列表和工时数量,然后调用 `calculate_cost` 函数计算总费用,并输出结果。