Question: Please write code in python The Currency/Volume Converter In a previous lesson we created a function named unit_converter that converted from USD/gallon to CAD/liter. The
Please write code in python




The Currency/Volume Converter In a previous lesson we created a function named unit_converter that converted from USD/gallon to CAD/liter. The issue with unit_converter is that it had a built in assumption -- regarding the input currency. We will fix this by adding another parameter to be explicit about the conversion. Both the 'from' currency and the 'to' currency will be added. Part 1 For this project, you must write the code for the following function: def unit_converter (units, total_cost, from_currency, to_currency): The function unit_converter converts the total_cost in from_currency to to_currency. units: number of gallons (for $) or liters (for C$) total_cost: USD dollars or CAD dollars paid for units from_currency: either '$' or 'C$' .to_currency: either '$' or 'C$' Here's a typical use: # CAD/liter to USD/gallon USD_per_gallon = unit_converter(61, 124.00, 'C$', 'S') # USD/gallon to CAD/liter CAD_per_liter = unit_converter(16, 45.0, 's', 'C$') You should use the same constant values for (unit conversions and exchange rates): LITERS_PER_GALLON GALLONS_PER_LITER = 3.78541 = 0.264172 # exchange rates CAD_TO_USD = 0.76 USD_TO_CAD = 1.31 You must handle all combinations of 's' and 'C$' for from_currency and to_currency. A '$' to 's' conversion would mean the answer is given in $/gallon. You can assume that if the from_currency is '$' then the units are gallons otherwise they are liters (we'll fix this later). The best strategy is to write the code that seems the clearest to you (perhaps using lots of if statements) and that it passes the tests. Write it so that you understand it. Don't continue until you pass the tests. Part 2 Once you get your code to work, rename the function unit_converter to unit_converter_v1 and now write a new unit_converter but now strive for the following: have one exit point (one return statement) use as few selection statements as possible (is only 2 if statements possible?) Try to notice patterns that encourage you to write the logic simpler (using less syntax). If you find yourself repeating a lot of the same code, see if you can use variables to factor out the stuff that changes from the stuff that does not. You will only be tested for a single unit_converter, but the goal is for you to write different versions of the same code to meet the above goals. Once you have one version working, you can use that to test your new versions to make sure you are getting the same values
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
