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.
Your program will prompt the user to enter the four required items. Each one must be done by a function:
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.
The calculation for CO2 produced should be done in two separate functions:
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.
Print the information matching the format in the example. You must define one more function to help with this:
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%