Beginner: Trying to get REAL NUMBERS from %d command

Vlastimil Brom vlastimil.brom at gmail.com
Sun Dec 30 18:00:13 EST 2012


2012/12/30 Alvaro Lacerda <alacerda617 at gmail.com>:
> 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



More information about the Python-list mailing list