python only prints integers

F

francesco

I'm pretty new in Python language. I have a problem with numbers: it
seems python doesn't know any more how to count!
I get only the down rounded integer
20/8 = 2
8/3=2
I probably changed some option to round the numbers, but I don't
remember how.
Is there a way to reset the number of digits to default?
Thanks in advance
 
G

geremy condra

I'm pretty new in Python language. I have a problem with numbers: it
seems python doesn't know any more how to count!
I get only the down rounded integer
20/8 = 2
8/3=2
I probably changed some option to round the numbers, but I don't
remember how.
Is there a way to reset the number of digits to default?
Thanks in advance

Use floats instead of integers:

Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.2.5


or use Python3:

Python 3.2a1 (r32a1:83318, Aug 13 2010, 22:32:03)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.2.5
 
I

Ian

I'm pretty new in Python language. I have a problem with numbers: it
seems python doesn't know any more how to count!
I get only the down rounded integer
20/8 = 2
8/3=2
I probably changed some option to round the numbers, but I don't
remember how.
Is there a way to reset the number of digits to default?

In Python 2, the '/' operator performs integer division by default
when both its operands are integers. To change this, either place
this at the top of the file:

from __future__ import division

or convert your numbers to floats:
2.5

In Python 3, the '/' operator always performs true division.
 
K

kost BebiX

Well, that's because 20 is integer. To get float you can write 20.0 (or 20.).
20.0/8.0 = 2.5
8.0/3.0 = 2.6666666666666665
 
F

francesco

In Python 2, the '/' operator performs integer division by default
when both its operands are integers.  To change this, either place
this at the top of the file:

from __future__ import division

or convert your numbers to floats:


2.5

In Python 3, the '/' operator always performs true division.

Thanks to all! Very quick answer!
I fixed the problem by using floats.
Thanks again
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top