Question: Python: Start with the code below This module provides a currency converter function that works with a global dictionary conversions = { USD:
Python: Start with the code below
""" This module provides a currency converter function that works with a global dictionary """ conversions = { "USD": 1, "EUR": .9, "CAD": 1.4, "GBP": .8, "CHF": .95, "NZD": 1.66, "AUD": 1.62, "JPY": 107.92 } def currency_converter(quantity: float, source_curr: str, target_curr: str): """ Convert from one unit of currency to another. Keyword arguments: quantity -- a float representing the amount of currency to be converted. source_curr -- a three letter currency identifier string from the conversions dictionary target_curr -- a three letter currency identifier string from the conversions dictionary """ return quantity / conversions[source_curr] * conversions[target_curr] def unit_test(): try: currency_converter(1, "USE", "USD") except KeyError: print("PASS: Invalid Source Currency Raises ValueError") else: print("FAIL: Invalid Source Currency Does Not Raise Error") try: currency_converter(1, "USD", "USE") except KeyError: print("PASS: Invalid Target Currency Raises ValueError") else: print("FAIL: Invalid Target Currency Does Not Raise ValueError") if currency_converter(10, "USD", "GBP") == 8: print("PASS: Conversion from USD to GBP") else: print("FAIL: Conversion from USD to GBP") if currency_converter(2.8, "CAD", "USD") == 2: print("PASS: Conversion from CAD to USD") else: print("FAIL: Conversion from CAD to USD") if currency_converter(1.8, "EUR", "CAD") == 2.8: print("PASS: Conversion from EUR to CAD") else: print("FAIL: Conversion from EUR to CAD") if __name__ == "__main__": unit_test()Add the following:
At the module level, create a global variable called home_currency and set it to the empty string.
Create a function called currency_options() with a string parameter base_curr. The function should print out a table of options for converting base_curr to all other currencies. Use quantities of the base currency from 10 to 90 and use the currency_converter() function to calculate all of the other currency values. Be sure to look at the sample run below for an example.
- Note that you must use f-strings to generate the lines of the table,
- Use a for loop to print the header line. Do not hard code the header. Hint: If you have typed 'EUR' anywhere in your currency_options function, you have hard-coding.
- You must use a for loop instead of writing a separate line of code for each of the nine lines. Do not hard code the lines. Hint again: If you have typed 'EUR' anywhere in your currency_options function, you have hard-coding.
- Do not use the home_currency global anywhere in the currency_options() function. home_currency should not appear anywhere in this function, use base_curr instead.
- The base_curr must appear as the first column, and should not be duplicated in the table. This will require use of a nested for loop, and the continue keyword might come in handy here.
- The columns of the table must be evenly spaced.
- You can choose to left-justify or right-justify the columns, but they must line up.
- All currency values should have two decimals of precision throughout the table (25.50 not 25.5 or 26).
Inside the main() function, before the menu is called, ask the user for their home currency. Do this in a loop until the user enters a valid string that is one of the entries in the conversions dictionary. Assign the user response to the home_currency global (be careful not to shadow the original home_currency name)
Finally, modify the menu() function so that the currency table is printed before entering the loop that manages the menu. To be clear:
- currency_options() should be called from menu(), not from main().
- The currency table should be printed just once during a run of the program.
Sample Run (your output can be different as long as it meets the criteria above) NZD AUD 20.75 20.25 41.50 40.50 62.25 60.75 Please enter your name: Eric Hi Eric, welcome to Foothill's database project. What is your home currency?MON What is your home currency?GBP Options for converting from GBP: GBP USD EUR CAD CHF JPY 10.00 12.50 11.25 17.50 11.88 1349.00 20.00 25.00 22.50 35.00 23.75 2698.00 30.00 37.50 33.75 52.50 35.62 4047.00 40.00 50.00 45.00 70.00 47.50 5396.00 50.00 62.50 56.25 87.50 59.38 6745.00 60.00 75.00 67.50 105.00 71.25 8094.00 70.00 87.50 78.75 122.50 83.12 9443.00 80.00 100.00 90.00 140.00 95.00 10792.00 90.00 112.50 101.25 157.50 106.88 12141.00 83.00 81.00 103.75 101.25 124.50 121.50 145.25 141.75 166.00 162.00 186.75 182.25 Sample Run (your output can be different as long as it meets the criteria above) NZD AUD 20.75 20.25 41.50 40.50 62.25 60.75 Please enter your name: Eric Hi Eric, welcome to Foothill's database project. What is your home currency?MON What is your home currency?GBP Options for converting from GBP: GBP USD EUR CAD CHF JPY 10.00 12.50 11.25 17.50 11.88 1349.00 20.00 25.00 22.50 35.00 23.75 2698.00 30.00 37.50 33.75 52.50 35.62 4047.00 40.00 50.00 45.00 70.00 47.50 5396.00 50.00 62.50 56.25 87.50 59.38 6745.00 60.00 75.00 67.50 105.00 71.25 8094.00 70.00 87.50 78.75 122.50 83.12 9443.00 80.00 100.00 90.00 140.00 95.00 10792.00 90.00 112.50 101.25 157.50 106.88 12141.00 83.00 81.00 103.75 101.25 124.50 121.50 145.25 141.75 166.00 162.00 186.75 182.25
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
