it seems that the for loop need to have integer arguments
is there another one where we can have a sep value like 0.1 which is not integer value
like for j in range ( 10.2 , 50, 5.5)
or what else should i use instead?
Thanks
it seems that the for loop need to have integer arguments
is there another one where we can have a sep value like 0.1 which is not integer value
like for j in range ( 10.2 , 50, 5.5)
or what else should i use instead?
Thanks
Here’s a really quick hack for float_range I just coded for fun:
def float_range(min, max, step=1.0):
ret = []
amount_of_values = (max-min) / step
step_amount = (max-min) / amount_of_values
for i in range(amount_of_values):
ret.append(min + step_amount * i)
return ret
for i in float_range(10.2, 50, 5.5):
print i
You can find another, perhaps more comprehensive implementation at http://code.activestate.com/recipes/66472/ .
What about this…
j = 1
j_end = 10
j_step = 0.5
while j < j_end:
j = j + j_step
print j
Another way (must have at least Python 2.3 or 2.4 (can’t recall when yield became a standard keyword)):
def frange(min, max, step = 1.0):
while min < max:
min += step
yield min
It doesn’t check for error values or anything, but that should be easy to add…
integers are nice but sometimes you deal with real numbers!
but is are the min and max again limited to integer
let say that i have a loop that is function of angles
let say from minang = 100.5 degrees to a max of 210.8 degrees
and i want to step with an increment of 1.1 degree
is the for loop able to deal with that of if i have to make an independant set of variables inside to deal with it ?
or may be the while loop is easier to deal with this kind of situtation
Thanks
I don’t think it matters as long as you don’t clobber your variables inside the loop.