[Tutor] range() fractional increment

Ionut Vancea ionut.vancea at gmail.com
Tue Mar 31 10:58:13 CEST 2009


Hi,

On Tue, Mar 31, 2009 at 4:44 AM, james carnell <jimcarnell at yahoo.com> wrote:
> 1) I feel dumb for asking this.
> 2) I looked for 20 minutes and didn't find an answer
>
> Trying to make a drawLine function in a 2d array.
>
> example:
> x0000   row = 25 : col = 10
> x0000   row = 26 : col = 10.3
> x0000   row = 27 : col = 10.6
> 0x000   row = 28 : col = 11
> 0x000   row = 29 : col = 11.3
> 0x000   row = 30 : col = 11.6
> 00x00   row = 31 : col = 12
>
> for row in range(25,31,1):
>     for col in range(10,12, 0.3):  #<- Crash Bang doesn't work 0.3 = zero =
> infinite loop?
>
> so then I tried...
>
>>>> c = 10
>>>> while(c < 12.3):
> ...     print c
> ...     c += 1.0/3.0
> ...
> 10
> 10.3333333333
> 10.6666666667
> 11.0
> 11.3333333333
> 11.6666666667
> 12.0
>
> is there no way to do it with a range function (and have it still look like
> you're not on crack)?

maybe you can use arange() from Numeric:

In [1]: from Numeric import *

In [2]: arange(-1,1,0.2)
Out[2]: array([-1. , -0.8, -0.6, -0.4, -0.2,  0. ,  0.2,  0.4,  0.6,  0.8])

and if you need a list use:

arange(-1,1,0.2).tolist()

Cheers,
-- 
===
Ioan Vancea
http://www.vioan.ro


More information about the Tutor mailing list