While Statement

Kushal Kumaran kushal.kumaran at gmail.com
Fri May 22 05:33:12 EDT 2009


On Fri, May 22, 2009 at 2:47 PM, Joel Ross <joelc at cognyx.com> wrote:
> Hi all,
>
> I have this piece of code
>
> class progess():
>
>    def __init__(self, number,  char):
>
>        total = number
>        percentage = number
>        while percentage > 0 :
>            percentage = int(number/total*100)
>            number-=1
>            char+="*"
>            print char
>
> progess(999,  "*")
>
> Just wondering if anyone has any ideas on way the percentage var gets set to
> the value 0 after the first loop.
>

Put in a

from __future__ import division

statement at the start.  You can experiment in the python shell if you'd like.

>>> 2/3
0
>>> from __future__ import division
>>> 2/3
0.66666666666666663
>>>

This kind of division is the default in Python 3.

-- 
kushal



More information about the Python-list mailing list