Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'value,': 0.04; 'string': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:Help': 0.11; 'def': 0.12; '"f"': 0.16; '"r")': 0.16; "'r'": 0.16; '9.0': 0.16; 'erroneously': 0.16; 'lambda': 0.16; 'message-id:@4ax.com': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'temp': 0.16; 'with?': 0.16; 'subject:python': 0.16; '>>>': 0.22; 'input': 0.22; "aren't": 0.24; 'url:home': 0.24; 'question': 0.24; 'header:X-Complaints-To:1': 0.27; 'wondering': 0.29; '"",': 0.31; 'keyerror:': 0.31; 'file': 0.32; '(most': 0.33; 'subject:with': 0.35; 'returning': 0.36; 'charset:us-ascii': 0.36; 'subject:?': 0.36; 'half': 0.37; 'received:76': 0.38; 'whatever': 0.38; 'to:addr:python-list': 0.38; 'recent': 0.39; 'generating': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'conversion': 0.61; 'making': 0.63; 'email addr:gmail.com': 0.63; 'these.': 0.91; '2013': 0.98 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dennis Lee Bieber Subject: Re: Help with python functions? Date: Tue, 01 Oct 2013 19:19:14 -0400 Organization: IISS Elusive Unicorn References: <5240489d$0$29992$c3e8da3$5496439d@news.astraweb.com> <3af43a58-fc56-46d1-9e1e-cb69f8aa520b@googlegroups.com> <5d5e5c4d-a79c-4b1a-b9da-ee2ad766ded8@googlegroups.com> <1b025b07-3c9c-4390-80e5-8b5661f00d96@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: adsl-76-249-20-155.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 6.00/32.1186 X-No-Archive: YES X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1380669562 news.xs4all.nl 16001 [2001:888:2000:d::a6]:39594 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:55251 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? ) 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 "", line 1, in File "", line 4, in temp KeyError: 'r' >>> -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/