loops
robert
no-spam at no-spam.invalid
Sat Oct 18 16:59:46 EDT 2008
Aaron Brady wrote:
> Gandalf wrote:
>
>> On Oct 18, 12:39 pm, Duncan Booth <duncan.bo... at invalid.invalid>
>> wrote:
>>> Gandalf <goldn... at gmail.com> wrote:
>>>> how can I do width python a normal for loop width tree conditions like
>>>> for example :
>>>> for x=1;x<=100;x+x:
>>>> print x
>>> What you wrote would appear to be an infinite loop so I'll assume you meant
>>> to assign something to x each time round the loop as well. The simple
>>> Python translation of what I think you meant would be:
>>>
>>> x = 1
>>> while x <= 100:
>>> print x
>>> x += x
>>>
>>> If you really insist on doing it with a for loop:
>>>
>>> def doubling(start, limit):
>>> x = start
>>> while x <= limit:
>>> yield x
>>> x += x
>>>
>>> ...
>>>
>>> for x in doubling(1, 100):
>>> print x
>> I was hopping to describe it with only one command. most of the
>> languages I know use this.
>> It seems weird to me their is no such thing in python. it's not that I
>> can't fined a solution it's all about saving code
>
> Do you anticipate reusing it? You could make something a little more
> extendable.
>
> for x in iexpression( 'x', 1, 100, 'x+x' ):
> print x
>
> or
>
> for x in iexpression( lambda x: x+x, 1, 100 ):
> print x
>
> I'm assuming you don't want or have a closed form, in this case x= 2**
> _x.
>
#and to learn even more about this, import this:
import this # ;-)
More information about the Python-list
mailing list