Store the conversion data from the csv file in some object (list of lists, dictionary of dictionaries, etc.)
- open the file in read mode
- save as a list of lines with f.readlines()
- exit the with/as statement
- create an empty list
- loop through the lines from the file
- for each line, remove the trailing new line character and split the line on the commas, which will make a list
- append the list to the empty list you made above – you should have a list of lists
Find the correct conversion factor from your data object and make conversion.
- Create a function to find conversion unit
- loop though the list of lists
- if the
from_unit
is equal to the item in the first position of the list and theto_unit
is equal to the third item in the list, return the second item in the list
- Create a function to convert between units
- the function should take the
test_value
andconversion_factor
as arguments and return the new value - call and test the function with your
test_value
andconversion factor
- the function should take the
- Finally, combines step 1 and step 2
- Use function from step 1 to find conversion unit and save that to a variable
- Use the output to apply conversion with function from step 2
- You can create a function that takes
from_unit
,to_unit
, andtest_value
as arguments for this step
- Print out a full sentence response with the final answer
- Look for potential errors and handle them
- Someone might give the initial value as a string instead of float/integer
- Attempt to convert the initial value to float
- Handle the error by printing out what's wrong with the input and end function
- Someone might request a final unit that is not in your data
- If after looping through the list of lists, there's no matching unit, print out the issue and end function
- Someone might give the initial value as a string instead of float/integer
- Run your code on the provided test examples