Beginner: Trying to get REAL NUMBERS from %d command

A

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

Vlastimil Brom

2012/12/30 Alvaro Lacerda said:
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);


Hi,
it seems, you are converting the numbers to integer decimals using %d
try simply %s (together with from __future__ import division
cf.:
http://docs.python.org/2/library/stdtypes.html#string-formatting-operations

hth,
vbr
 
P

Peter Otten

Alvaro said:
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:

2.5

So the caculation part works.
'2'

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

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

See <http://docs.python.org/2/library/stdtypes.html#string-formatting>
for more.
 
H

Hans Mulder

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.


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
 
A

Alvaro Lacerda

%s got the job done!!!

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top