[Tutor] String Attribute
alan.gauld at btinternet.com
Sat Aug 1 22:40:36 CEST 2015
On 01/08/15 19:48, Ltc Hotspot wrote:
> There is an indent message in the revised code.
> Question: Where should I indent the code line for the loop?
Do you understand the role of indentation in Python?
Everything in the indented block is part of the structure,
so you need to indent everything that should be executed
as part of the logical block.
> fname = raw_input("Enter file name: ")
> if len(fname) < 1 : fname = "mbox-short.txt"
> fh = open(fname)
> count = 0
> addresses = set()
> for line in fh:
> if line.startswith('From'):
> line2 = line.strip()
> line3 = line2.split()
> line4 = line3
> count = count + 1
Everything after the if line should be indented an extra level
because you only want to do those things if the line
And note that, as I suspected, you are adding the whole line
to the set when you should only be adding the address.
(ie line4). This would be more obvious if you had
used meaningful variable names such as:
strippedLine = line.strip()
tokens = strippedLine.split()
addr = tokens
Could you please delete the extra lines from your messages.
Some people pay by the byte and don't want to receive kilobytes
of stuff they have already seen multiple times.
Author of the Learn to Program web site
Follow my photo-blog on Flickr at:
More information about the Tutor