Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #55251
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Subject | Re: Help with python functions? |
| Date | 2013-10-01 19:19 -0400 |
| Organization | IISS Elusive Unicorn |
| References | (3 earlier) <l1qsdl$f3k$1@dont-email.me> <5d5e5c4d-a79c-4b1a-b9da-ee2ad766ded8@googlegroups.com> <l1s8tj$4ob$1@dont-email.me> <l1spuh$4ob$4@dont-email.me> <1b025b07-3c9c-4390-80e5-8b5661f00d96@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.580.1380669562.18130.python-list@python.org> (permalink) |
On Tue, 1 Oct 2013 10:53:26 -0700 (PDT), kjakupak@gmail.com declaimed the
following:
>I ended up with these. I know they're only like half right...
>I was wondering if any of you had to do this, what would you end up with?
>
># Question 1.a
Well... Off-hand you aren't making use of "to_unit" -- For Celsius
input you are automatically returning Fahrenheit; for Kelvin input you are
erroneously adding the C->K conversion value, and for Fahrenheit you
automatically generating Celsius. If "from_unit" is not in the set "CcKkFf"
(no Rankene measure? <G>) you are returning whatever "to_unit" contained...
>>> to_base = {"c" : lambda x : x + 273.15,
... "f" : lambda x : (5.0 * (x - 32.0) / 9.0) + 273.15,
... "k" : lambda x : x }
>>> from_base = {"c" : lambda x : x - 273.15,
... "f" : lambda x : (x - 273.15) * 9.0 / 5.0 + 32.0,
... "k" : lambda x : x }
>>> def temp(T, from_unit, to_unit):
... fu = from_unit.lower()[0]
... tu = to_unit.lower()[0] # presumes both are string
... return from_base[tu]( to_base[fu]( T ) )
...
>>> temp(32, "F", "C")
0.0
>>> temp(0, "C", "K")
273.15
>>>
>>> temp(0, "C", "F")
32.0
>>> temp(212, "F", "C")
100.0
>>> temp(212, "F", "K")
373.15
>>>
>>> temp(100, "F", "R")
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "<interactive input>", line 4, in temp
KeyError: 'r'
>>>
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Help with python functions? kjakupak@gmail.com - 2013-09-23 05:57 -0700
Re: Help with python functions? Roy Smith <roy@panix.com> - 2013-09-23 09:11 -0400
Re: Help with python functions? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-23 13:56 +0000
Re: Help with python functions? kjakupak@gmail.com - 2013-09-23 15:32 -0700
Re: Help with python functions? Terry Reedy <tjreedy@udel.edu> - 2013-09-23 18:48 -0400
Re: Help with python functions? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-24 03:08 +0000
Re: Help with python functions? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-24 03:17 +0000
Re: Help with python functions? giacomo boffi <pecore@pascolo.net> - 2013-09-24 18:53 +0200
Re: Help with python functions? MRAB <python@mrabarnett.plus.com> - 2013-09-24 18:18 +0100
Re: Help with python functions? kjakupak@gmail.com - 2013-09-23 15:55 -0700
Re: Help with python functions? Dave Angel <davea@davea.name> - 2013-09-24 00:07 +0000
Re: Help with python functions? kjakupak@gmail.com - 2013-09-23 18:23 -0700
Re: Help with python functions? Dave Angel <davea@davea.name> - 2013-09-24 03:52 +0000
Re: Help with python functions? Denis McMahon <denismfmcmahon@gmail.com> - 2013-09-24 02:12 +0000
Re: Help with python functions? kjakupak@gmail.com - 2013-09-23 19:40 -0700
Re: Help with python functions? Denis McMahon <denismfmcmahon@gmail.com> - 2013-09-24 14:51 +0000
Re: Help with python functions? Denis McMahon <denismfmcmahon@gmail.com> - 2013-09-24 19:42 +0000
Re: Help with python functions? kjakupak@gmail.com - 2013-10-01 10:53 -0700
Re: Help with python functions? random832@fastmail.us - 2013-10-01 15:02 -0400
Re: Help with python functions? Denis McMahon <denismfmcmahon@gmail.com> - 2013-10-01 21:45 +0000
Re: Help with python functions? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-10-01 19:19 -0400
Re: Help with python functions? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-24 03:15 +0000
Re: Help with python functions? Denis McMahon <denismfmcmahon@gmail.com> - 2013-09-24 15:02 +0000
csiph-web