Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > python only prints integers

Reply
Thread Tools

python only prints integers

 
 
francesco
Guest
Posts: n/a
 
      01-06-2011
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
 
Reply With Quote
 
 
 
 
geremy condra
Guest
Posts: n/a
 
      01-06-2011
On Thu, Jan 6, 2011 at 2:49 PM, francesco
<(E-Mail Removed)> wrote:
> 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.
>>> 20/8

2
>>> 20.0/8

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.
>>> 20/8

2.5
>>> 20.0/8

2.5
 
Reply With Quote
 
 
 
 
Ian
Guest
Posts: n/a
 
      01-06-2011
On Jan 6, 3:49*pm, francesco <(E-Mail Removed)> wrote:
> 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:

>>> 20.0 / 8.0

2.5
>>> float(20) / float(

2.5

In Python 3, the '/' operator always performs true division.
 
Reply With Quote
 
kost BebiX
Guest
Posts: n/a
 
      01-06-2011
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

07.01.2011, 00:49, "francesco" <(E-Mail Removed)>:
> 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
>
> --
> http://mail.python.org/mailman/listinfo/python-list


--
jabber: http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
francesco
Guest
Posts: n/a
 
      01-06-2011
On 6 Gen, 23:59, Ian <(E-Mail Removed)> wrote:
> On Jan 6, 3:49*pm, francesco <(E-Mail Removed)> wrote:
>
> > 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:
>
> >>> 20.0 / 8.0

> 2.5
> >>> float(20) / float(

>
> 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
 
Reply With Quote
 
Edward A. Falk
Guest
Posts: n/a
 
      01-07-2011
In article <(E-Mail Removed)>,
Ian <(E-Mail Removed)> wrote:
>
>In Python 3, the '/' operator always performs true division.


How can I get integer division?

--
-Ed Falk, (E-Mail Removed)
http://thespamdiaries.blogspot.com/
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      01-07-2011
On Fri, 07 Jan 2011 03:32:54 +0000, Edward A. Falk wrote:

> In article
> <(E-Mail Removed)>, Ian
> <(E-Mail Removed)> wrote:
>>
>>In Python 3, the '/' operator always performs true division.

>
> How can I get integer division?



>>> 25//4

6


--
Steven
 
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
Can Home Produced Prints Equal Lab Prints? Denis Boisclair Digital Photography 8 11-13-2004 03:20 PM
Durability of inkjet prints, photo prints Bill Tuthill Digital Photography 45 05-02-2004 03:04 AM
Just recieved my 15 free digital prints from Kellards- Amazing Quality Prints! Bruce Digital Photography 5 12-29-2003 02:49 AM
Why the prints from film feel more 3D then prints from digital. Victor81 Digital Photography 35 12-12-2003 07:49 AM
Epson 1280 prints NOT Waterproof, 2200 prints ARE, Right? Dr. Slick Digital Photography 12 11-29-2003 10:19 AM



Advertisments