…
…
Write functions that convert temperature, length, and currency; build a router that picks the right conversion, print a formatted table, and check it all with your own assert tests.
You will write a small version of the converter apps on your phone: Celsius to Fahrenheit, kilometers to miles, and dollars to foreign currencies. Each conversion will be its own function. Then you will pick the right conversion from the source and target unit and print the result in a formatted way, and at the end you will write your own tests.
Lessons used: numbers, f-strings and field width (3–4), conditions and None (6–8), for and range (10), functions, default and named arguments (14–16), tests with assert (19).
The exchange rates in the project are sample values, not current rates.
What you'll do
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
Celsius to Fahrenheit: F = C × 9 / 5 + 32. The reverse: C = (F - 32) × 5 / 9.
Write both functions. Round the result to one decimal place and return it (do not print). The lines that call the function do the printing.
This step's code cell is for members.Sign up free
1 mile = 1.609344 km. Instead of typing this number everywhere, keep it in a constant: MILE_KM = 1.609344 (as in lesson 16: values that never change get an uppercase name).
Write km_to_miles and miles_to_km, and round the result to two decimal places.
This step's code cell is for members.Sign up free
The rates sit in three constants: how many dollars 1 unit of a foreign currency is worth.
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
find_rate(unit)NoneNoneconvert_currency(amount, unit, direction="usd_to_foreign") gets the rate with find_rate. To convert dollars to a foreign currency you divide; to convert a foreign currency to dollars you multiply. The direction parameter has a default value (lesson 15): if you leave it out, "usd_to_foreign" is assumed. The other direction is "foreign_to_usd"; the last call passes it as a named argument.
This step's code cell is for members.Sign up free
The converter now takes a value, a source unit, and a target unit: 100, "C", "F" or 1000, "USD", "EUR". convert(value, source, target) decides which conversion function to call with an elif chain and returns the result; for an unknown conversion it returns None. You check whether a unit is a foreign currency with find_rate(unit) is not None.
report(value, source, target) calls convert and prints the result. Splitting a program into single-job functions like this is the separation from lesson 16: one function calculates, the other prints.
This step's code cell is for members.Sign up free
For a weather dashboard, print a Celsius-to-Fahrenheit table from -10 °C to 40 °C in steps of 10 degrees. range(-10, 41, 10) produces these values; the stop is written as 41 because the stop is not included (lesson 10).
Align the columns: Celsius in a field 6 characters wide, Fahrenheit in a field 8 characters wide, right-aligned, with one decimal place. The format is the same field width as in lesson 4: :>6.1f.
This step's code cell is for members.Sign up free
The converter seems to work, but how will you know whether you broke something when you change a formula? Write a test function like the one in lesson 19: test_converter() with assert lines inside. Work out the expected values by hand:
c_to_f(100) == 212.0, miles_to_km(10) == 16.09c_to_f(-40) == -40.0 (the point where the two scales meet), f_to_c(32) == 0.0convert_currency(11, "EUR") == 10.0, convert_currency(2, "CAD", direction="foreign_to_usd") == 1.5convert(10, "kg", "lb") is NoneWrite at least 5 assert lines and test every kind of conversion (temperature, length, currency, convert) at least once. The check will also run your tests against deliberately broken functions (for example, a c_to_f that forgets the +32) and see whether they catch the error.
This step's code cell is for members.Sign up free