for with decimal values?

Steven D'Aprano steve at
Sun May 3 19:52:29 EDT 2009

On Sun, 03 May 2009 13:41:49 -0700, Zentrader wrote:

> There is no need for a function or a generator.  A for() loop is a
> unique case of a while loop
> ## for i in range(-10.5, 10.5, 0.1):
> ctr = -10.5
> while ctr < 10.5:
>    print ctr
>    ctr += 0.1

To match the semantics of range(), the final value to ctr must be less 
than but not equal to 10.5. 

>>> print ctr

Your solution does not do this -- it goes one steps too many, and then 
misleadingly fails to print the value. This is a bug waiting to hit, any 
time somebody runs your loop then goes to use ctr outside the loop.


More information about the Python-list mailing list