What am I missing in this code, does it look good and properly formatted?

Viewed 1336

Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order. Uppercase words should take precedence over lowercase words. For example, 'Z' comes before 'a'.

The input file can contain one or more sentences, or be a multiline list of words.

"the quick brown fox jumps over the lazy dog"

textFileName = input("Enter the input file name: ")
textFile = open(textFileName, 'r')
listOfWords = []   
while True:
        line = textFile.readline()
        if line == "":
            break
        else:
            words = line.split()
            for word in words:
                listOfWords.append(word)
                listOfWords.sort()
                uniqueListOfWords = []
                for count in range(len(listOfWords) -1):
                    if listOfWords[count]!= listOfWords[count + 1]:
                        uniqueListOfWords.append(listOfWords[count])
                        uniqueListOfWords.append(listOfWords[len(listOfWords) -1])
                        print(word)
2 Answers

Firstly, when reading the file, you can do something much simpler to get a list of every consecutive word. I.E.,

fname = input("Enter the input filename: ") # get the filename

f = open(fname, 'r') # open the file
contents = f.read() # read the file contents
f.close() # close the file (you didn't before)

all_words = contents.split(' ') # get every word in original order

As for the sorting, that seems to be fine. However, when creating a unique word list that contains all unique values, a simple trick that you can use is this: unique = list(set(sorted(all_words))). In short, a set type can only have 1 of each unique value. So if you convert it to a set and then back to a list, you get a list of all unique values in alphabetical order. The sorted function returns a sorted version of a list, whereas list.sort sorts the list, and returns nothing.

Finally, remember to always close a file if it hasn't been already.

On top of that, consider commenting some code to help future readers understand what each part of it means, and include some whitespace (i.e., spaces, newlines) every now and then to seperate different sections and to make it look just a bit nicer. That's all I can think of, however.

EDIT: I realize I should include a summarized version of the modified code, so here it is:

fname = input("Enter the input filename: ") # get the filename

f = open(fname, 'r') # open the file
contents = f.read() # read the file contents
f.close()

all_words = contents.split(' ') # get every word in original order
unique_words = list( set( sorted( all_words ) ) ) # sort the words, then convert it to a set type which can only contain 1 of each unique value, and convert it back to a list

for word in unique_words:
    print word # print every unique word

I hope I edited as you mentioned, but this worked for me after some changes to my code.

def sorted_unique_file(input_filename): 

    input_file = open(input_filename, 'r') #open file in read mode

    file_contents = input_file.read() #read text from file

    input_file.close()
    
    unique = []

    word_list = file_contents.split()

    for word in word_list:

        if word not in unique:

            unique.append(word) #add unique words into list

    print("unique words in alphabetical order are: ")

    print("\n".join(sorted(list(set(unique))))) #sort the words using set operation

def main():

    filename = input("Enter the input file name: ") #read the file name

    sorted_unique_file(filename) #call the function to print unique words in alphabetical order

if __name__ == "__main__":

    main()
Related