[Tutor] Mailbox

Cameron Simpson cs at zip.com.au
Thu Jul 30 04:01:05 CEST 2015


On 29Jul2015 21:55, ltc.hotspot at gmail.com <ltc.hotspot at gmail.com> wrote:
>I have a second and unrelated question:
>
>I tried to work backward  to see if there is a logic error associated with a variable is being skipped over:
>
>#top of code, initialize variable
>output_list = ["default"]

Curious: why not just make this an empty list: []

[...snip...]
>count = 0
>fname = raw_input("Enter file name: ")
>if len(fname) < 1 : fname = "mbox-short.txt"
>for line in fname:
>    line = line.strip()
>    if not line.startswith('From '): continue
>    line = line.split()
>count = count + 1
>print len(line)
>fh = open(fname)
>print "There were", count, "lines in the file with From as the first word"
[...snip...]

My first observation would be that "count = count + 1" is not inside the loop.  
That means that it fires just once, _after_ the loop. So it will always be 1.

Also, what is the purpose of this line:

  line = line.split()

Cheers,
Cameron Simpson <cs at zip.com.au>


More information about the Tutor mailing list