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


Groups > comp.lang.python > #55235

Re: Help with python functions?

From random832@fastmail.us
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>
Subject Re: Help with python functions?
Date 2013-10-01 15:02 -0400
Newsgroups comp.lang.python
Message-ID <mailman.568.1380654155.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Tue, Oct 1, 2013, at 13:53, kjakupak@gmail.com wrote:
> 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
> def temp(T, from_unit, to_unit):

Assuming this is temperature conversion. You should add a docstring.

>     if from_unit == 'C' or from_unit == 'c':

Consider normalizing the unit with .upper() at the top of the function
so you don't have to do this "or" case in every single section.

>         return 32 + (9/5)*T

You are ignoring the value of to_unit and returning the value in
fahrenheit. 

>     elif from_unit == 'K' or from_unit == 'k':
>         return T + 273.15

This conversion is simply wrong - it's the conversion _from_ celsius
_to_ kelvin.

>     elif from_unit == 'F' or from_unit == 'f':
>         return (5/9)*(T - 32)

You are ignoring the value of to_unit and returning the value in celsius

>     else:
>         return to_unit

I don't know what this is. It's probably wrong.


To implement a temperature conversion function, I would convert from the
unit given to kelvin, then convert from kelvin to the desired unit -
that way you don't have to implement every combination separately.

> # Question 1.b
> def comp(T1, u1, T2, u2):
>     if u1 != u2:
>         T1 = temp(T1, u1)

You're not passing in u2 here.

>     elif T2 > T1:
>         return -1
>     elif T1 > T2:
>         return 1
>     else:
>         return 0

> # Question 2
> def P(p_0, t, i):
>     Amount = P(1 + (i/100))
>     return P(1 + (t * i/12))

I don't know what this is.

Is this for homework?

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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