None???

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • News

    None???

    I just started using python a few weeks ago. This is my first program. You
    input your income and what percent you want to save then it tells you how
    much to put away. The the code works, but it puts in the word "none" after
    it calls the function. Is the call sytax incorrect? I'd appreciate any
    feedback you could offer .

    # savings.py 09-14-03

    print "Hello, welcome to my savings program."
    print
    print "I will ask you a few questions then calculate"
    print "the info and give you a plan of action."
    print
    first_name = raw_input("What is your first name? ")
    print
    last_name = raw_input("What is your last name? ")
    print
    income = input("How much do you make a year after taxes? ")
    print
    percent = input("What percent of your income do you want to put aside? ")
    print

    weekly_income = income/52

    def compute(income, percent):
    result = income*percent/100
    print result

    print "Ok,",first_nam e + " " + last_name
    print
    print "This is how much you should put away a week:"
    print "$",compute(wee kly_income, percent)
    print
    print "If you stick to this you will go places!!! :-)"




  • Heather Coppersmith

    #2
    Re: None???

    On Sun, 21 Sep 2003 01:12:04 GMT,
    "News" <jakle1@hotmail .com> wrote:
    [color=blue]
    > I just started using python a few weeks ago. This is my first
    > program. You input your income and what percent you want to save
    > then it tells you how much to put away. The the code works, but
    > it puts in the word "none" after it calls the function. Is the
    > call sytax incorrect? I'd appreciate any feedback you could
    > offer .[/color]

    [ ... ]
    [color=blue]
    > weekly_income = income/52[/color]
    [color=blue]
    > def compute(income, percent):
    > result = income*percent/100
    > print result[/color]

    Note that this function prints its result and then returns None.
    [color=blue]
    > print "Ok,",first_nam e + " " + last_name
    > print
    > print "This is how much you should put away a week:"
    > print "$",compute(wee kly_income, percent)[/color]

    OTOH, it looks like this print statement expects the function to
    return its result.
    [color=blue]
    > print
    > print "If you stick to this you will go places!!! :-)"[/color]

    The simplest (and most general) solution would seem to be to
    define the function to return its result:

    define compute(income, percent):
    result = income * percent / 100
    return result

    Then the print statement will work as written.

    Regards,
    Heather

    --
    Heather Coppersmith
    That's not right; that's not even wrong. -- Wolfgang Pauli

    Comment

    • Gregor Lingl

      #3
      Re: None???

      Heather Coppersmith schrieb:
      [color=blue]
      >[color=green]
      >>weekly_inco me = income/52[/color]
      >
      >[color=green]
      >>def compute(income, percent):
      >> result = income*percent/100
      >> print result[/color]
      >[/color]
      Additionally please notice, that your program will produce
      incorrect and probably at first irritating results:

      **** example output: ****

      How much do you make a year after taxes? 5199

      What percent of your income do you want to put aside? 1

      Ok, Gregor Lingl

      This is how much you should put away a week:
      $ 0

      ****

      This comes from using integer-constants together with
      inputting integers, which causes the /-operator to
      perform integer division.

      A simple way to avoid this would be to use 52.0 and
      100.0 instead of 52 and 100

      Regards,
      Gregor

      Comment

      • David Eppstein

        #4
        Re: None???

        In article <3F6D68AA.90706 03@aon.at>, Gregor Lingl <glingl@aon.a t>
        wrote:
        [color=blue][color=green][color=darkred]
        > >>def compute(income, percent):
        > >> result = income*percent/100
        > >> print result[/color]
        > >[/color]
        > Additionally please notice, that your program will produce
        > incorrect and probably at first irritating results:[/color]
        ....[color=blue]
        > This comes from using integer-constants together with
        > inputting integers, which causes the /-operator to
        > perform integer division.[/color]

        Arguably, any code that uses / with integer arguments is now buggy,
        because it will produce different results in different Python versions.
        You should either use // (integer division) or force floating point
        division (e.g. by changing the expression to income*percent/100.0).

        --
        David Eppstein http://www.ics.uci.edu/~eppstein/
        Univ. of California, Irvine, School of Information & Computer Science

        Comment

        • Gerrit Holl

          #5
          Re: None???

          David Eppstein wrote:[color=blue]
          > In article <3F6D68AA.90706 03@aon.at>, Gregor Lingl <glingl@aon.a t>
          > wrote:[color=green]
          > > This comes from using integer-constants together with
          > > inputting integers, which causes the /-operator to
          > > perform integer division.[/color]
          >
          > Arguably, any code that uses / with integer arguments is now buggy,
          > because it will produce different results in different Python versions.
          > You should either use // (integer division) or force floating point
          > division (e.g. by changing the expression to income*percent/100.0).[/color]

          Or, of course, use from __future__ import division. That is always
          the first line of any of my files, I can't live without it :)

          Gerrit.

          --
          208. If he was a freed man, he shall pay one-third of a mina.
          -- 1780 BC, Hammurabi, Code of Law
          --
          Asperger Syndroom - een persoonlijke benadering:

          Het zijn tijden om je zelf met politiek te bemoeien:
          De website van de Socialistische Partij (SP) in Nederland: Informatie, nieuws, agenda en publicaties.


          Comment

          • Tim Rowe

            #6
            Re: None???

            On Sun, 21 Sep 2003 11:13:47 -0700, David Eppstein
            <eppstein@ics.u ci.edu> wrote:
            [color=blue]
            >Arguably, any code that uses / with integer arguments is now buggy,
            >because it will produce different results in different Python versions.
            >You should either use // (integer division) or force floating point
            >division (e.g. by changing the expression to income*percent/100.0).[/color]

            What's the behaviour of "from __future__ import division" on versions
            that are too old to know about the future of division?

            Comment

            • David Eppstein

              #7
              Re: None???

              In article <4l4umv4kl82d8t 5td29in26cfb57c tb6h0@4ax.com>,
              Tim Rowe <tim@remove_if_ not_spam.digiti g.co.uk> wrote:
              [color=blue][color=green]
              > >Arguably, any code that uses / with integer arguments is now buggy,
              > >because it will produce different results in different Python versions.
              > >You should either use // (integer division) or force floating point
              > >division (e.g. by changing the expression to income*percent/100.0).[/color]
              >
              > What's the behaviour of "from __future__ import division" on versions
              > that are too old to know about the future of division?[/color]

              A nice error message.
              So that's another good way to go.

              --
              David Eppstein http://www.ics.uci.edu/~eppstein/
              Univ. of California, Irvine, School of Information & Computer Science

              Comment

              Working...