Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed4.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.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; '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; 'jan': 0.12; "('c',": 0.16; '23,': 0.16; '32)': 0.16; '32,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'tests': 0.22; 'header:User-Agent:1': 0.23; 'mon,': 0.24; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; '-0700,': 0.31; "d'aprano": 0.31; 'sep': 0.31; 'steven': 0.31; 'yourself.': 0.31; 'skip:c 30': 0.32; 'run': 0.32; 'monday,': 0.33; 'subject:with': 0.35; 'something': 0.35; 'next': 0.36; 'subject:?': 0.36; 'similar': 0.36; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'received:173': 0.61; "you're": 0.61; 'email addr:gmail.com': 0.63; 'received:fios.verizon.net': 0.84; 'careful': 0.91; '2013': 0.98 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Help with python functions? Date: Mon, 23 Sep 2013 18:48:20 -0400 References: <5240489d$0$29992$c3e8da3$5496439d@news.astraweb.com> <66aa7d75-a819-4b21-9f1e-7ad265996150@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.0 In-Reply-To: <66aa7d75-a819-4b21-9f1e-7ad265996150@googlegroups.com> 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: 32 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1379976512 news.xs4all.nl 15968 [2001:888:2000:d::a6]:33563 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:54664 On 9/23/2013 6:32 PM, kjakupak@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) What happens if you run some tests? If you use unittest, you can use the assertAlmostEqualMethod, or just write something similar yourself. Be careful with values near 0.. At minimum, how many tests do you need, 6 or 9? -- Terry Jan Reedy