[Tutor] search through a list

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Mon Jul 4 09:34:15 CEST 2005

> #this function returns the number of lines on record for each log file.
> def line_finder(LogsInDir, flag):
>     flag=flag.strip()
>     return_next=False
>     for line in LogsInDir:
>         if return_next:
>             return line.strip()
>     else:
>         if line.strip() == flag:
>             return_next = True

Hi Nephish,

Question: what should the program return if return_next is never True?
That is, what happens if the program does not find the flag that you're
looking for?

The comment of this function also seems a bit weird: it appears to try to
return the very next line after the 'flag' line of a file: it's not doing
any counting of numbers, as far as I can tell.

> the line in question is this one:
> NumLinesOnFile=line_finder(LogsRead, LogsInDir[NextLog])
> print NumLinesOnFile
> if i run this in idle printing NumLinesOnFile gives me a string 14

The function will return None if 'flag' can't be found in the LogsRead
list.  Otherwise, it'll return one of the lines of the file.

So I'd assume for the moment that line_finder can't find what it is trying
to find.  You may want to test this yourself by adding some kind of
debugging statement in line_finder() to make it more clear if t can't find
what it's looking for.  Does this sound reasonable?

Best of wishes to you!

More information about the Tutor mailing list