Are Electric Vehicles (EVs) really better for the environment than gasoline-powered vehicles? Yes. But how much better depends on how the electricity you give it was made. If you get the power from solar panels on your own roof, you are making absolutely zero greenhouse gas emissions when you drive. If you get it mainly from coal-burning power plants, you will still have lower overall emissions, but the difference will be much smaller than if you get it from a place that has a lot of renewable energy generators like hydropower, wind, and solar.

In this assignment you will write a tool that can compare an EV against a gas vehicle for a given state and annual mileage. It will read in each of these four items, calculate the estimated CO2 emissions for each vehicle, and show the totals and difference. You get a few functions for free to help you:

function getWHPM (ev) contains a dictionary defining several EV models and their efficiency, expressed in watt-hours per mile. You pass in a string as a parameter. If the string is found in the dictionary, the function returns its paired WHPM value. If not, it returns -1.

function getMPG (car) works the same way, but this dictionary pairs gas-powered vehicle models with their efficiency in miles per gallon of gas (MPG).

function getCO2state (state) also works the same way. Its dictionary pairs two-letter state abbreviations with the average amount of CO2 per kWh of electricity generated in that region. It returns a number which is metric tons of CO2 per kWh. As with the other functions, it returns -1 if the state passed in is not in the dictionary.

Input

Your program will prompt the user to enter the four required items. Each one must be done by a function:

  • inputev (), inputgascar (), inputstate (), inputmiles ()

They all work the same way. Prompt the user. Input a string. Check it and make sure it's valid. If not, keep looping to prompt the user again until a valid input is given. That valid input is the return value of the function.

For the first three, you can check if the input string is valid by calling the helper function I gave you. If the result of the function is -1, you know you have to prompt the user again.

For the last one, you must first make sure it's a number that they typed, and then that it's a positive number. (Refer to the Strings chapter for a refresher on how to do these two things.) Your program must not crash just because someone enters text that's not a number.

Calculations

The calculation for CO2 produced should be done in two separate functions:

  • gasolineCO2 (mpg, miles) both integers; mpg comes from the helper function. returns CO2 in metric tons.
  • elecCO2 (whpm, state, miles) whpm comes from the helper function, state is a string, miles is an integer. returns CO2 in metric tons.

For a gasoline car, the formula for burning a gallon of gas is on this web page:

https://www.epa.gov/energy/greenhouse-gases-equivalencies-calculator-calculations-and-references

For an EV, you know the watt-hours-per-mile, and the miles, so you can calculate how many watts the car uses to drive that far (and then convert as 1000 Wh = 1 kWh). Then, for a given state, you look up how many metric tons of CO2 are produced per kWh and multiply.

Output

Print the information matching the format in the example. You must define one more function to help with this:

  • printinfo (astring, afloat) prints one line of output: astring is left-justified at 35 characters, followed by afloat which is 8 characters and two digits after the decimal point. The function makes it easy for the EV and gas car results to be formatted identically so they line up nicely. Then print out the amount of CO2 saved both as a number and as a percentage (show these with one digit after decimal point).

Sample Input:

Bolt
HR-V
TX
10000

Sample Output:

Input EV name:
Input gasoline car name:
Input state:
Input miles driven:
Gas CO2 produced per year: 2.96 Tons
EV CO2 produced per year: 1.04 Tons
Driving Bolt instead of HR-V
saves 1.9 tons of CO2 per year
a reduction of 64.9%
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.