indentations is 4 spaces,it work with 2 and 8 but never ever us anything else than 4.
A simple fix move line 8 so it get input from loop.
Remove set
import sys
result = []
f = open("sample_text.txt")
for line in f:
for word in line.split():
if word.startswith('h'):
result.append(word)
result_length = len(result)
print "Total DISTINCT words starting with 'z': ", result_length
Here is the script with right indent,and som print to see what happends.
my sample_text.txt hi this is an test.
hi again why not.
hi number 3.
result = []
f = open("sample_text.txt")
for line in f:
for word in line.split():
if word.startswith('h'):
result.append(word)
print result # use print as help easier to see what happends
print len(result) # use print as help easier to see what happends
print "Total DISTINCT words starting with '%s' is %d" % (result[0][0], len(result))
'''
my output-->
['hi']
1
['hi', 'hi']
2
['hi', 'hi', 'hi']
3
Total DISTINCT words starting with 'h' is 3
''' snippsat
Practically a Posting Shark
808 posts since Aug 2008
Reputation Points: 353
Solved Threads: 294