Problem: Open the file week.txt and read it line by line. For each line, split the line into a list of words using the split() function. The program should build a list of words. For each word on each line check to see if the word is already in the list and if not append it to the list. When the program completes, sort and print the resulting words in alphabetical order. You can download the sample data at http://www.pythonlearn.com/code/romeo.txt
The file of week4.txt is the one I download from the website (remeo.txt) and name as week4.txt
fh = open(fname)
wordlist = []
x = 0
for line in fh:
line = line.rstrip()
words = line.split()
wordsnum = len(words)
while x < wordsnum:
if x-1<1:
firstword = words[x]
wordlist = wordlist.append(firstword)
x = x+1
continue
newword = words[x];
if newword == words[x-1]:continue
wordlist = wordlist.append(newword)
x = x+1
wordlist = wordlist.sort()
print wordlist
I am having a problem with the append statement. Could you help me with this problem? Thank you very much!