如何获取一组单词并在输出中按字母顺序打印?

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

pickle.load(f)`


婷婷同学_
浏览 91回答 1
1回答

千巷猫影

问题是只输入了一个单词并且对其调用了排序方法。让我们获取多个单词,将它们附加到列表中并对它们进行排序:input_words = []# Get input wordsword = 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 wordsinput_words.sort()# Print sorted list of wordsprint(input_words)# Write to filewith open('your_file.txt', 'w') as f:    for item in input_words:        f.write("%s\n" % item)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python