Help with python functions?

Dave Angel davea at davea.name
Mon Sep 23 20:07:44 EDT 2013


On 23/9/2013 18:55, kjakupak at gmail.com wrote:

> On Monday, September 23, 2013 9:56:45 AM UTC-4, Steven D'Aprano wrote: 
>> On Mon, 23 Sep 2013 05:57:34 -0700, kjakupak wrote: 
>> 
>> Now you're done! On to the next function... 
>> 
>> 
>> 
>> -- 
>> 
>> Steven 
>
> 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? 
> Also, the temperature number had to be of type float so I feel like I did this wrong...
>
> As for the next one, so far I've gotten:
> def comp(T1, u1, T2, u2):
>     if u1 > u2:
>         return -1
>     elif u2 > u1:
>         return 1
>     else:
>         return 0


I didn't see any spec that said Python 3.x.  in version 2.x, this would
be incorrect.

-- 
DaveA





More information about the Python-list mailing list