Velocity Reviews > Beginner: Trying to get REAL NUMBERS from %d command

# Beginner: Trying to get REAL NUMBERS from %d command

Alvaro Lacerda
Guest
Posts: n/a

 12-30-2012
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);

Joel Goldstick
Guest
Posts: n/a

 12-30-2012
On Sun, Dec 30, 2012 at 5:54 PM, Joel Goldstick <(E-Mail Removed)>wrote:

>
>
>
> On Sun, Dec 30, 2012 at 5:37 PM, Alvaro Lacerda <(E-Mail Removed)>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

Irmen de Jong
Guest
Posts: n/a

 12-30-2012
On 30-12-2012 23:37, Alvaro Lacerda wrote:
>
> I'm trying to get full number result using the %d command

Try %f instead. %d is the formatting symbol for integer numbers.
See http://docs.python.org/2/library/std...ing-operations

Or have a look at what string.format() can do:
http://docs.python.org/2/library/str...ormat-examples

-irmen

Vlastimil Brom
Guest
Posts: n/a

 12-30-2012
2012/12/30 Alvaro Lacerda <(E-Mail Removed)>:
> 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/std...ing-operations

hth,
vbr

Peter Otten
Guest
Posts: n/a

 12-30-2012
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.

Hans Mulder
Guest
Posts: n/a

 12-30-2012
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

Alvaro Lacerda
Guest
Posts: n/a

 12-30-2012
%s got the job done!!!

Thank you all for the info and links,
I appreciate it!

 Thread Tools

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post jimpooler General Computer Support 0 11-27-2009 06:57 AM Subra C Programming 25 03-08-2007 01:31 AM Please Computer Support 4 02-14-2007 12:01 AM Curious Trigger ASP .Net 2 09-09-2006 10:59 PM mort Digital Photography 3 02-16-2005 01:43 AM

Advertisments