Homework 2: Temperature Conversion

Temperature Conversion

Here are four commonly-used units of temperature:

A standard first program to give to students is to ask them to convert between these temperatures. That's what we'll be doing.

Your program should input a temperature as a floating point number with an appended unit letter. It should then print the temperature in all four of the units above, as a floating point number with a unit letter. Printed temperatures should have one decimal point of precision. For this assignment only, assume that the user of the program won't make a mistake, and will always enter a valid number immediately followed by a valid unit letter.

Here's a sample run from my version of the program:

    Enter a temperature with units F, C, K or R (e.g. 32.1F): 212F

Some hints on writing your conversion program:

  • The easiest way to proceed is to convert whatever temperature is input into K, and then convert that back to the four different output temperatures. That way you only have to write 6 lines of conversion instead of 12.

    The formula for conversion are as follows:

    • K = C + 273.15
    • K = (F - 32) * 5 / 9 + 273.15
    • K = R * 5 / 9
    • C = K - 273.15
    • F = (K - 273.15) * 9 / 5 + 32
    • R = K * 9 / 5
  • A function that might be useful to you when writing this, beyond the ones documented in the book, is round().

    • round(x, 1) rounds the floating point value x to 1 decimal place.
  • You will probably want to use string subscripting and array slicing to split the number from the unit indicator.

    numberStr = inputStr[0:-1]
    unitStr = inputStr[-1]

Your assignment is to write and test a program that meets the specification. Submit the program as a text file called "temperature.py". Submit a test run as a text file called "tests.txt". Also include a text file called "writeup.txt" containing a writeup that contains a brief note about what you did, what problems you had, and what you learned.