脚本中的函数定义必须出现在文件的结尾。 请将 "HeatingCarbonEmissions" 函数定义后面的所有语句都移到第一个局部函数定义前面。
时间: 2024-05-14 19:19:50 浏览: 103
好的,以下是修改后的代码:
```python
def HeatingCarbonEmissions(heating_type, energy_efficiency_rating):
if heating_type == "gas":
if energy_efficiency_rating == "A":
return 0.18
elif energy_efficiency_rating == "B":
return 0.23
elif energy_efficiency_rating == "C":
return 0.27
else:
return 0.32
elif heating_type == "electric":
if energy_efficiency_rating == "A":
return 0.22
elif energy_efficiency_rating == "B":
return 0.28
elif energy_efficiency_rating == "C":
return 0.33
else:
return 0.39
else:
return 0
def main():
print("Welcome to the carbon emissions calculator!")
print("This calculator will estimate the amount of carbon emissions generated by your household.")
print("Please enter the following information:")
num_people = int(input("Number of people in your household: "))
num_cars = int(input("Number of cars owned by your household: "))
miles_per_car = float(input("Average miles driven per car per year: "))
public_transport_usage = int(input("Average usage of public transport per week (in hours): "))
train_usage = int(input("Average train usage per week (in hours): "))
bus_usage = int(input("Average bus usage per week (in hours): "))
heating_type = input("Type of heating used in your household (gas/electric): ")
energy_efficiency_rating = input("Energy efficiency rating of your heating system (A/B/C/D): ")
total_carbon_emissions = CarCarbonEmissions(num_people, num_cars, miles_per_car) + PublicTransportCarbonEmissions(public_transport_usage) + TrainCarbonEmissions(train_usage) + BusCarbonEmissions(bus_usage) + HeatingCarbonEmissions(heating_type, energy_efficiency_rating)
print("Your household generates an estimated", round(total_carbon_emissions, 2), "tonnes of carbon emissions per year.")
if __name__ == "__main__":
main()
```
阅读全文