PEP 284, Integer for-loops

Greg Ewing greg at cosc.canterbury.ac.nz
Thu Mar 7 20:12:54 EST 2002


David Eppstein wrote:
> 
> > > for x in range(n-1,-1,-2):
> >
> > for 0 <= i < n/2:
> >   x = 2*i + 1
> >   ...
> 
> Not quite the same, if n is odd.

You're right. (Actually, it steps the wrong way too!)

There are various ways of fixing it, but I'm not
going to bother presenting one. In real life, I would
take a step back and look at the problem being solved,
in order to find the clearest way of expressing it.
That's impossible in this case, since it's a
contrived example.

If someone can present a realistic problem requiring
a loop of that sort, I may be able to suggest something.

-- 
Greg Ewing, Computer Science Dept, University of Canterbury,	  
Christchurch, New Zealand
To get my email address, please visit my web page:	  
http://www.cosc.canterbury.ac.nz/~greg



More information about the Python-list mailing list