Help with python functions?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Sep 23 23:17:46 EDT 2013


On Mon, 23 Sep 2013 15:32:37 -0700, kjakupak wrote:

> def temp(T, from_unit, to_unit):
>     conversion_table = {('c', 'k'):lambda x: x + 273.15,
>                         ('c', 'f'):lambda x: (x * (9.0/5)) + 32, ('k',
>                         'c'):lambda x: x - 273.15,
>                         ('k', 'f'):lambda x: (x * (9.0/5)) - 459.67,
>                         ('f', 'c'):lambda x: (x - 32) * (5.0/9), ('f',
>                         'k'):lambda x: (x + 459.67) * (5.0/9)}
>     f = conversion_table[(from_unit.lower(), to_unit.lower())] return
>     f(T)
> 
> Would this be correct?

Oh, I forgot... what happens if both units are the same?

Hint: if both units are the same, no conversion is necessary.




-- 
Steven



More information about the Python-list mailing list