[Tutor] Need help w/ a for loop

Kent Johnson kent37 at tds.net
Thu Oct 23 18:30:21 CEST 2008


On Thu, Oct 23, 2008 at 10:20 AM, bob gailer <bgailer at gmail.com> wrote:

> import operator
> ops = (operator.add, operator.sub)
> x = 0
> m = 0
> for in in range...
>   x = ops[m](x, 4.0/i)
>   m =  1-m

itertools.cycle(ops) is handy here. Hmm, there is a cute one-line
solution (excluding import and input) that uses itertools.cycle() and
zip()...not sure if this is a homework problem so I won't post it
yet...

Kent


More information about the Tutor mailing list