Advertisement
Guest User

temperature conversion function

a guest
Jan 19th, 2013
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.42 KB | None | 0 0
  1. conversions = {
  2.             'Celsius': (1, 0),
  3.             'Farenheit': (1.8, 32),
  4.             'Kelvin': (1, 273.15)
  5.         }
  6. def convert_temp(temp, from_unit, to_unit):
  7.     try:
  8.         divide, subtract = conversions[from_unit]
  9.         multiply, add = conversions[to_unit]
  10.     except KeyError:
  11.         raise KeyError('Unknown unit %s, %s' % from_unit, to_unit)
  12.     return (temp - subtract) * multiply / divide + add
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement