[Tutor] Silly loop question

mailing list ml.cyresse at gmail.com
Sat Aug 20 03:12:10 CEST 2005


Hehe, 

Yah, I should've read the tutorial again, I do apologise, it was a 3
in the morning-coffee not working question.

Regards, 


Liam Clarke

On 8/17/05, Alan G <alan.gauld at freenet.co.uk> wrote:
> > changeIndex = None
> > al = len(line)
> > for i in range(al):
> 
> And theres the problem.
> Pythonprovides two loops, for is really a *foreach* so if you don't
> want to process *each* item - eg by jumping forward - then you really
> should use while and manage the index thataway.
> 
> You can hack about with the indexin a for but basically its abusing
> the intent of a for loop. Beter to explicitly hack about in a while
> loop!
> 
> So:
> 
> while i < len(line):
>    if changeIndex and i < changeIndex
>       i = changeIndex
> 
>    if line[i] == "{":
>       nextRightBracket = line.find("}",i)
>       #other stuff happens
>       changeIndex = nextRightBracket + 1
>    else:
>       i += 1
> 
> > read Alan's tutorial again. *grin*
> 
> Umm, well the section on branching does refer to the two loops and
> when
> each is most appropriate, so...  :-)
> 
> Alan G
> Author of the Learn to Program web tutor
> http://www.freenetpages.co.uk/hp/alan.gauld
> 
>


More information about the Tutor mailing list