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

pickle.load(f)`

  • What is your problem? – clubby789 Sep 16 '20 at 19:24
  • sort works on arrays – roshnet Sep 16 '20 at 19:25
  • Welcome to S.O. Nikan! Please post the code you have tried so far so we can easily see what your issue is. – lime Sep 16 '20 at 19:26
  • Please supply the expected [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). Show where the intermediate results differ from what you expected. We should be able to copy and paste a contiguous block of your code, execute that file, and reproduce your problem along with tracing output for the problem points. – Prune Sep 16 '20 at 19:26
  • Also note that we expect you to research solutions on your own, before posting here. There are many tutorials on sorting. – Prune Sep 16 '20 at 19:26
  • This inputs a single word, not a list of words. – John Gordon Sep 16 '20 at 19:30

1 Answers1

1

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 word\n')
# 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 word\n')

# 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("%s\n" % item)
Aviv Yaniv
  • 6,188
  • 3
  • 7
  • 22