酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

写一个管理水电用量水电费用的代码

以下是一个使用 Python 编写的简单示例代码,用于管理水电用量和费用的计算: ```python class UtilityManager: def __init__(self, water_rate, electricity_rate): self.water_rate = water_rate # 每吨水的价格 self.electricity_rate = electricity_rate # 每度电的价格 self.water_usage = 0 # 累计用水量 self.electricity_usage = 0 # 累计用电量 def add_water_usage(self, usage): self.water_usage += usage def add_electricity_usage(self, usage): self.electricity_usage += usage def calculate_total_cost(self): water_cost = self.water_usage * self.water_rate electricity_cost = self.electricity_usage * self.electricity_rate total_cost = water_cost + electricity_cost return total_cost # 假设每吨水 2 元,每度电 0.5 元 manager = UtilityManager(2, 0.5) # 本月用水量 10 吨 manager.add_water_usage(10) # 本月用电量 200 度 manager.add_electricity_usage(200) # 计算总费用 total_cost = manager.calculate_total_cost() print(f"本月水电费总费用:{total_cost} 元") ``` 在上述代码中,我们创建了一个 `UtilityManager` 类来管理水电用量和费用。您可以根据实际的价格和用量数据进行修改和扩展。 [2024-10-29 21:27:01 | AI写代码神器 | 356点数解答]

相关提问