Help with python functions?

MRAB python at mrabarnett.plus.com
Tue Sep 24 13:18:04 EDT 2013


On 24/09/2013 17:53, giacomo boffi wrote:
> kjakupak at gmail.com writes:
>
>> 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?
>
> not always:
>
>>>> temp(-300.0, 'c', 'k')
> -26.850000000000023
>>>>
>
In other words, it depends what you mean by 'correct'.

Zero Kelvin ("Absolute Zero") is the lowest possible temperature; in
reality there's no such temperature as -300°C.




More information about the Python-list mailing list