While Statement

J

Joel Ross

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.

Any feed back would be appreciated.

Regards

jross
 
K

Kushal Kumaran

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.

This kind of division is the default in Python 3.
 
A

Andre Engels

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.

Any feed back would be appreciated.

In Python 2.6 and lower, division of two integers gives an integer,
being the result without rest of division with rest:

In your example, the second run has number = 998, total = 999. 998/999
is evaluated to be zero.

There are two ways to change this:
1. Ensure that at least one of the things you are using in the
division is a float. You could for example replace "total = number" by
"total = float(number)" or "number/total*100" by
"float(number)/total*100" or by "(number*100.0)/total".
2. Use Python 3 behaviour here, which is done by putting the import
"from __future__ import division" in your code.
 
J

Joel Ross

Joel said:
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.

Any feed back would be appreciated.

Regards

jross

Thanks for the quick response and the heads up. Makes sense

Thank you. You guys helped me out alot:)
 
J

Joel Ross

Andre said:
In Python 2.6 and lower, division of two integers gives an integer,
being the result without rest of division with rest:

2

In your example, the second run has number = 998, total = 999. 998/999
is evaluated to be zero.

There are two ways to change this:
1. Ensure that at least one of the things you are using in the
division is a float. You could for example replace "total = number" by
"total = float(number)" or "number/total*100" by
"float(number)/total*100" or by "(number*100.0)/total".
2. Use Python 3 behaviour here, which is done by putting the import
"from __future__ import division" in your code.

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?
 
A

Andre Engels

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
 
J

Joel Ross

Andre said:
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
changed it to "float(number)/total*100" and it worked thanks for all
your help appreciated

thanks again
 
T

Tim Wintle

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:

I'd use:

(number * 100.)/total

- works because
<int> * <float> => <float>

It's a minor thing, but it's much faster to cast implicitly as you miss
the python function call overhead - it's no extra work to write, and for
numerical things it can really speed things up.
3.6434230804443359

Tim W
 
D

Dave Angel

Tim said:
I'd use:

(number * 100.)/total

- works because
<int> * <float> => <float>

It's a minor thing, but it's much faster to cast implicitly as you miss
the python function call overhead - it's no extra work to write, and for
numerical things it can really speed things up.


3.6434230804443359

Tim W
It's the old-timer in me, but I'd avoid the float entirely. You start
with ints, and you want to end with ints. So simply do the multiply
first, then the divide.

number * 100/total

will get the same answer.
 
J

Joel Ross

Dave said:
It's the old-timer in me, but I'd avoid the float entirely. You start
with ints, and you want to end with ints. So simply do the multiply
first, then the divide.

number * 100/total

will get the same answer.

Cheers thanks for the support. if it works faster I'll definitely do it
 
T

Tim Wintle

It's the old-timer in me, but I'd avoid the float entirely. You start
with ints, and you want to end with ints. So simply do the multiply
first, then the divide.

number * 100/total

Agreed, to be honest I'd not fully read the original post and didn't
realise everything was an int.
 
J

J. Cliff Dyer

It's the old-timer in me, but I'd avoid the float entirely. You start
with ints, and you want to end with ints. So simply do the multiply
first, then the divide.

number * 100/total

will get the same answer.

Also, to get the same behavior (faster integer division) in python 3 or
when using `from __future__ import division`, use the // division
operator instead of /.

In fact, a little experimentation shows that you get the speedup even
when using python 2.6 with old-style division. Probably because python
doesn't have to check the types of its arguments before deciding to do
integer division.
.... each.timeit(22222222)
....
2.3681092262268066
2.417525053024292
0.81031703948974609
0.81548619270324707


Cheers,
Cliff
 
M

Mike Kazantsev

changed it to "float(number)/total*100" and it worked thanks for all
your help appreciated

I believe operator.truediv function also deserves a mention here, since
line "op.truediv(number, total) * 100" somehow seem to make more sense
to me than an explicit conversion.
There's also "op.itruediv" for "number /= float(total) * 100" case.

http://docs.python.org/dev/library/operator.html

--
Mike Kazantsev // fraggod.net

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.11 (GNU/Linux)

iEYEARECAAYFAkoXeGEACgkQASbOZpzyXnEzqgCgnVBO2PueqWqZOARjsBZaj2M0
oT0An2q9SfJ1OF4NbBCSsBPQpTtJdYyh
=0gkd
-----END PGP SIGNATURE-----
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top