Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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);




 
Reply With Quote
 
 
 
 
Joel Goldstick
Guest
Posts: n/a
 
      12-30-2012
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

 
Reply With Quote
 
 
 
 
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


 
Reply With Quote
 
Vlastimil Brom
Guest
Posts: n/a
 
      12-30-2012
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/std...ing-operations

hth,
vbr
 
Reply With Quote
 
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.

 
Reply With Quote
 
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

 
Reply With Quote
 
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!
 
Reply With Quote
 
 
 
Reply

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 Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it possilbe to get the real 5.1 real sound output for my DM500S? jimpooler General Computer Support 0 11-27-2009 06:57 AM
Finding 1000 largest numbers from a file having some billion numbers Subra C Programming 25 03-08-2007 01:31 AM
How can I get a non 25x80 command window when I use the "at" command on XP? Please Computer Support 4 02-14-2007 12:01 AM
Detailsview shows real numbers with commata: Error converting data type nvarchar to real! Curious Trigger ASP .Net 2 09-09-2006 10:59 PM
Frame Numbers vs. JPEG Numbers With CF Cards mort Digital Photography 3 02-16-2005 01:43 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57