Question: Write a program in python that will calculate the users carbon footprint for the year. You should have a different function for each component of
Write a program in python that will calculate the users carbon footprint for the year. You should have a different function for each component of the footprint. The user will input the several parts of their footprint: how the house is heated (wood, gas, propane, or electricity), car fuel (no car, electric, gas, diesel), then the relevant information. The relevant information needed will vary depending on the users answer, and the calculations of the total carbon footprint will also vary. This is a complex problem - it is best to work on it one function at a time, see if it is working, and then move forward. You will need many different if statements. Write at least two tests for each function.
Framework for code:
import test def heating_footprint(heat_source, fuel_used): # Calculate the heating carbon footprint. Return 0 if heat_source is wood or electric. # Wood is renewable. Electricity will be calculated under the electricity section. # For propane, 12.7 lbs CO2 / gal. For natural gas, 11.7 lbs CO2/ therm. CO2_emitted = 0 return CO2_emitted
def electrical_footprint(KWH_used): # calculate the electrical carbon footprint. In Iowa, 1 kwh from the grid releases 1.0985 lb CO2. # This varies across the US depending on how much coal, natural gas, nuclear, hydro, wind, and solar are used CO2_emitted = 0 return CO2_emitted
def driving_footprint(driving_mode, miles_driven, mpg): # calculates the driving carbon footprint. We will assume that electric vehicles are only charged at home. # If electric, this part of the carbon calculator can be 0, as it will be calculated under the electrical_footprint # gas = 19.6 lbs CO2 per gallon. # diesel = 22.38 lbs CO2 per gallon. CO2_emitted = 0 return CO2_emitted
def flying_footprint(miles_flown): # calculates the flying carbon footprint. Use .309 lbs CO2 per mile travelled, which is an average. # This is a simplification: longer flights are more efficient as take-off uses much more fuel. CO2_emitted = 0 return CO2_emitted
def main(): hm = input("How do you heat your home: enter P for propane, W for wood, E for electricity, or G for natural gas.") dm = input("What fuel does your car use: enter N for no car, E for electric, G for gas, D for diesel")
# You'll need other inputs. You should make floats (especially for heat_fuel_used) heat_fuel_used = 0 # You'll need to ask for this. It will be gallons for propane, therms for natural gas. 0 for electricity or wood # You should use a different input statement depending on the fuel source.
kilowatt_hours_used = 0 # You'll need to ask for this. miles_driven = 0 # you'll need to ask for this, unless they do not have a car. miles_per_gallon = 1 # you'll need to ask for this, unless their car is electric or no car miles_flown = 0 #you'll need to ask for this.
total_CO2 = 0 # call the different functions. # Print the total CO2 emissions from home heating, electricity use, driving, and flying # using easy to understand print statement. (Printing a number with nothing else is NOT easy to understand! # Print the total CO2 emitted.
print("Testing heat function") test.testEqual(heating_footprint("W", 0),0) test.testEqual(heating_footprint("E", 0), 0) test.testEqual(heating_footprint("G", 660),7722,0) test.testEqual(heating_footprint("P", 867), 11010.9,1) return
main()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
