[Tutor] Silly loop question
Alan G
alan.gauld at freenet.co.uk
Tue Aug 16 18:26:36 CEST 2005
> 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