Skip to content

How to get a set of words and print in alphabetical order in the output? [closed]

import pickle`f=open('/home/nikan/Desktop/Linux os/text/word.txt', 'rb')

pickle.load(f)`

Answer

The problem was that only a single word was inputted and the sort method has been invoked on it.

Let’s get multiple words, append them to a list and sort them:

input_words = []
# Get input words
word = input('Please enter wordn')
# As long as word is not "stop"
while word != 'stop':
    # Get new input word
    input_words.append(word)
    # Append to list of words
    word = input('Please enter wordn')
# Sort words
input_words.sort()
# Print sorted list of words
print(input_words)
# Write to file
with open('your_file.txt', 'w') as f:
    for item in input_words:
        f.write("%sn" % item)