Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #54631 > unrolled thread

Help with python functions?

Started bykjakupak@gmail.com
First post2013-09-23 05:57 -0700
Last post2013-09-24 15:02 +0000
Articles 3 on this page of 23 — 10 participants

Back to article view | Back to comp.lang.python


Contents

  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

Page 2 of 2 — ← Prev page 1 [2]


#55251

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2013-10-01 19:19 -0400
Message-ID<mailman.580.1380669562.18130.python-list@python.org>
In reply to#55231
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/

[toc] | [prev] | [next] | [standalone]


#54674

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-09-24 03:15 +0000
Message-ID<524103cb$0$29992$c3e8da3$5496439d@news.astraweb.com>
In reply to#54665
On Mon, 23 Sep 2013 15:55:53 -0700, kjakupak wrote:


> 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


That is only comparing the units, not the temperatures. Since the units 
are characters, you're essentially saying that any temperature in Kelvin 
is always greater than any temperature in Celsius, since "K" > "C". 
Worse, you're saying that any two temperatures with the same unit are 
automatically equal, so that -50°F == 50000°F.

Instead, you need to convert both temperatures into a common unit, say, 
Kelvin, then compare the two temperatures:

* convert T1 from u1 to Kelvin;
* convert T2 from u2 to Kelvin;
* compare T1 and T2.

You don't have to use Kelvin. You could use any temperature scale, so 
long as it is the same for both temperatures.


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#54704

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2013-09-24 15:02 +0000
Message-ID<l1s9i4$4ob$2@dont-email.me>
In reply to#54674
On Tue, 24 Sep 2013 03:15:23 +0000, Steven D'Aprano wrote:

> You don't have to use Kelvin. You could use any temperature scale, so
> long as it is the same for both temperatures.

Given that he already has a handy conversion function to call, he should 
be able to convert t2 into the units of t1 if they're in different units 
(2 lines), and then do his comparison (5 lines).

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.python


csiph-web