[Tutor] Not Storing State

Alan Gauld alan.gauld at btinternet.com
Sat Feb 28 02:03:35 CET 2009


"Stephen Nelson-Smith" <sanelson at gmail.com> wrote

> I want to iterate over a bunch of lines; 

for line in <bunch of lines>

> If any line contains a certain string, I want to do something, 
> otherwise do something else.
 
if <certain string> in line:
      doSomething()
else:
      doSomethingElse()

> I can store state  - eg line 1 - did it contain the string? no.. ok
> we're cool, next line....

No need, just translate what you asked into Python code directly.

You don't say what kind of "bunch" your lines are in but the above 
will work with an open file or a list or a tuple etc.

HTH,

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list