Beginner: Trying to get REAL NUMBERS from %d command

Discussion in 'Python' started by Alvaro Lacerda, Dec 30, 2012.

  1. The code I wrote is supposed to ask the user to enter a number;
    Then tell the user what's going to happen to that number (x / 2 + 5) ;
    Then give the user an answer;

    I succeeded getting results from even numbers, but when I try diving an uneven number (i.e. 5) by 2, I get only the whole number (i.e. 2)

    I'm trying to get full number result using the %d command, I've tried to add the line "from __future__ import division" to the beginning of my code, but I haven't been succeeded.

    I also tried making my numbers decimals (i.e. 2.0 instead of just 2)


    Below is my program and thanks for the help :)



    from __future__ import division;

    def decimal_percent_test():
    number = input("Enter a number: ");
    print number, "will be divided by 2 then added by 5!";
    print " %d divided by %d is %d plus %d is... %d !" %(number,2,number/2,5,number/2+5);
    Alvaro Lacerda, Dec 30, 2012
    #1
    1. Advertising

  2. On Sun, Dec 30, 2012 at 5:54 PM, Joel Goldstick <>wrote:

    >
    >
    >
    > On Sun, Dec 30, 2012 at 5:37 PM, Alvaro Lacerda <>wrote:
    >
    >> The code I wrote is supposed to ask the user to enter a number;
    >> Then tell the user what's going to happen to that number (x / 2 + 5) ;
    >> Then give the user an answer;
    >>

    >
    > Try x / 2.5 + 5
    >


    oops I mean x / 2.0 + 5

    >
    >> I succeeded getting results from even numbers, but when I try diving an
    >> uneven number (i.e. 5) by 2, I get only the whole number (i.e. 2)
    >>
    >> I'm trying to get full number result using the %d command, I've tried to
    >> add the line "from __future__ import division" to the beginning of my code,
    >> but I haven't been succeeded.
    >>
    >> I also tried making my numbers decimals (i.e. 2.0 instead of just 2)
    >>
    >>
    >> Below is my program and thanks for the help :)
    >>
    >>
    >>
    >> from __future__ import division;
    >>
    >> def decimal_percent_test():
    >> number = input("Enter a number: ");
    >> print number, "will be divided by 2 then added by 5!";
    >> print " %d divided by %d is %d plus %d is... %d !"
    >> %(number,2,number/2,5,number/2+5);
    >>
    >>
    >>
    >>
    >> --
    >> http://mail.python.org/mailman/listinfo/python-list
    >>

    >
    >
    >
    > --
    > Joel Goldstick
    >




    --
    Joel Goldstick
    Joel Goldstick, Dec 30, 2012
    #2
    1. Advertising

  3. Irmen de Jong, Dec 30, 2012
    #3
  4. 2012/12/30 Alvaro Lacerda <>:
    > The code I wrote is supposed to ask the user to enter a number;
    > Then tell the user what's going to happen to that number (x / 2 + 5) ;
    > Then give the user an answer;
    >
    > I succeeded getting results from even numbers, but when I try diving an uneven number (i.e. 5) by 2, I get only the whole number (i.e. 2)
    >
    > I'm trying to get full number result using the %d command, I've tried to add the line "from __future__ import division" to the beginning of my code, but I haven't been succeeded.
    >
    > I also tried making my numbers decimals (i.e. 2.0 instead of just 2)
    >
    >
    > Below is my program and thanks for the help :)
    >
    > from __future__ import division;
    >
    > def decimal_percent_test():
    > number = input("Enter a number: ");
    > print number, "will be divided by 2 then added by 5!";
    > print " %d divided by %d is %d plus %d is... %d !" %(number,2,number/2,5,number/2+5);
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list



    Hi,
    it seems, you are converting the numbers to integer decimals using %d
    try simply %s (together with from __future__ import division
    cf.:
    >>> "%d" % (1.5,)

    '1'
    >>> "%s" % (1.5,)

    '1.5'
    >>>


    http://docs.python.org/2/library/stdtypes.html#string-formatting-operations

    hth,
    vbr
    Vlastimil Brom, Dec 30, 2012
    #4
  5. Alvaro Lacerda

    Peter Otten Guest

    Alvaro Lacerda wrote:

    > The code I wrote is supposed to ask the user to enter a number;
    > Then tell the user what's going to happen to that number (x / 2 + 5) ;
    > Then give the user an answer;
    >
    > I succeeded getting results from even numbers, but when I try diving an
    > uneven number (i.e. 5) by 2, I get only the whole number (i.e. 2)
    >
    > I'm trying to get full number result using the %d command, I've tried to
    > add the line "from __future__ import division" to the beginning of my
    > code, but I haven't been succeeded.
    >
    > I also tried making my numbers decimals (i.e. 2.0 instead of just 2)
    >
    >
    > Below is my program and thanks for the help :)
    >
    >
    >
    > from __future__ import division;
    >
    > def decimal_percent_test():
    > number = input("Enter a number: ");
    > print number, "will be divided by 2 then added by 5!";
    > print " %d divided by %d is %d plus %d is... %d !"
    > %(number,2,number/2,5,number/2+5);


    Let's try it in the interactive interpreter:


    >>> from __future__ import division
    >>> 5/2

    2.5

    So the caculation part works.

    >>> "%d" % 2.5

    '2'

    But the decimal part is dropped by the formatting operation. You have to use
    another format. The simplest is %s:

    >>> "%s" % 2.5

    '2.5'

    %f is also possible and offers more control over the output:

    >>> "%f" % 2.5

    '2.500000'
    >>> "%.2f" % 2.5

    '2.50'

    See <http://docs.python.org/2/library/stdtypes.html#string-formatting>
    for more.
    Peter Otten, Dec 30, 2012
    #5
  6. Alvaro Lacerda

    Hans Mulder Guest

    Hello,

    Python does not support REAL numbers. It has float number, which
    are approximations of real numbers. They behave almost, but not
    quite, like you might expect.

    It also has Decimal numbers. They also approximate real numbers,
    but slightly differently. They might behave more like you'd expect
    if you're used to an electronic calculator.


    On 30/12/12 23:37:53, Alvaro Lacerda wrote:
    > The code I wrote is supposed to ask the user to enter a number;
    > Then tell the user what's going to happen to that number (x / 2 + 5) ;
    > Then give the user an answer;
    >
    > I succeeded getting results from even numbers, but when I try diving
    > an uneven number (i.e. 5) by 2, I get only the whole number (i.e. 2)
    >
    > I'm trying to get full number result using the %d command, I've tried
    > to add the line "from __future__ import division" to the beginning of
    > my code, but I haven't been succeeded.
    >
    > I also tried making my numbers decimals (i.e. 2.0 instead of just 2)


    Errrhm, 2.0 is a float.

    To get decimal numbers, you'd need to import the Decimal module.



    > Below is my program and thanks for the help :)
    >
    >
    >
    > from __future__ import division;
    >
    > def decimal_percent_test():
    > number = input("Enter a number: ");
    > print number, "will be divided by 2 then added by 5!";
    > print " %d divided by %d is %d plus %d is... %d !" %(
    > number,2,number/2,5,number/2+5);


    The %d code will convert your number to a whole number.
    as you found out.

    The simplest way to make this program work, is to use the %s
    code, which doesn't convert to a different type of number:

    from __future__ import division;

    def decimal_percent_test():
    number = input("Enter a number: ")
    print number, "will be divided by 2 then added by 5!"
    print " %s divided by %s is %s plus %s is... %s !" % (
    number, 2, number/2, 5, number/2+5)

    while True:
    decimal_percent_test()


    Enter a number: 10
    10 will be divided by 2 then added by 5!
    10 divided by 2 is 5.0 plus 5 is... 10.0 !
    Enter a number: 11
    11 will be divided by 2 then added by 5!
    11 divided by 2 is 5.5 plus 5 is... 10.5 !
    Enter a number: x
    Traceback (most recent call last):
    File "test.py", line 11, in <module>
    decimal_percent_test()
    File "test.py", line 5, in decimal_percent_test
    number = input("Enter a number: ")
    File "<string>", line 1, in <module>
    NameError: name 'x' is not defined



    Hope this helps,

    -- HansM
    Hans Mulder, Dec 30, 2012
    #6
  7. %s got the job done!!!

    Thank you all for the info and links,
    I appreciate it!
    Alvaro Lacerda, Dec 30, 2012
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeremy Pyle

    Re: Representation of real numbers

    Jeremy Pyle, Jun 27, 2003, in forum: VHDL
    Replies:
    1
    Views:
    1,274
    Ken Smith
    Jun 27, 2003
  2. Jim Lewis

    Re: Representation of real numbers

    Jim Lewis, Jul 1, 2003, in forum: VHDL
    Replies:
    0
    Views:
    1,828
    Jim Lewis
    Jul 1, 2003
  3. senthil
    Replies:
    5
    Views:
    1,380
    senthil
    Jan 24, 2004
  4. Curious Trigger
    Replies:
    2
    Views:
    1,813
    Curious Trigger
    Sep 9, 2006
  5. CSZ
    Replies:
    2
    Views:
    70
    Eric I.
    Mar 21, 2007
Loading...

Share This Page