Hello,
Making the transition from Perl to Python, and have a
question about constructing a loop that uses an iterator
of type float. How does one do this in Python?
In Perl this construct quite easy:
for (my $i=0.25; $i=2.25; $i+=0.25) {
printf %9.2f\n, $i;
}
Thanks in advance for your help.
drife wrote:
Hello,
Making the transition from Perl to Python, and have a
question about constructing a loop that uses an iterator
of type float. How does one do this in Python?
Use a generator:
def iterfloat(start, stop, inc):
... f = start
... while f = stop:
... yield f
drife wrote:
Hello,
Making the transition from Perl to Python, and have a
question about constructing a loop that uses an iterator
of type float. How does one do this in Python?
In Perl this construct quite easy:
for (my $i=0.25; $i=2.25; $i+=0.25) {
printf %9.2f\n, $i;
}
=Py2.3:
Mark McEahern wrote:
drife wrote:
Hello,
Making the transition from Perl to Python, and have a
question about constructing a loop that uses an iterator
of type float. How does one do this in Python?
Use a generator:
def iterfloat(start, stop, inc):
... f = start
... while f = stop:
Mike Meyer wrote:
Or - and much safer when dealing with floating point numbers - iterate
over integers and generate your float values:
for j in range(1, 9):
i = j * .25
print %9.2f % i
There's a glitch there, though - should be range(1, 10).
Reinhold
PS: I'm wondering whether my
Mike Meyer wrote:
Or - and much safer when dealing with floating point numbers - iterate
over integers and generate your float values:
for j in range(1, 9):
i = j * .25
print %9.2f % i
I agree with this suggestion. As an historical aside, Fortran had loops
with floating point variables