While Statement

Andre Engels andreengels at gmail.com
Fri May 22 07:19:06 EDT 2009


On Fri, May 22, 2009 at 12:35 PM, Joel Ross <joelc at cognyx.com> wrote:

> Im using 2.6 python and when running this
>
> class progess():
>
>    def __init__(self, number, total,  char):
>
>        percentage = float(number/total*100)
>        percentage = int(round(percentage))
>        char = char * percentage
>        print char
>
> progess(100, 999,  "*")
>
> the "percentage = float(number/total*100)" always equals 0.0
> any ideas way this would be?

You have to make the conversion of integer to float before the
division. What you are calculating now is (take number = 998, total =
999)

number/total = 998/999 = 0
number/total*100 = 0*100 = 0
float(number/total*100) = float(0) = 0.0

Change "float(number/total*100)" to "float(number)/total*100" and it
should work:

float(number) = float(998) = 998.0
float(number)/total = 998.0/999 = 0.99899899899899902
float(number)/total*100 = 0.99899899899899902 * 100 = 99.899899899899902



-- 
André Engels, andreengels at gmail.com



More information about the Python-list mailing list