How do I print a list of words from a separate text file? I want to print all the words unless the word has a length of 4 characters.
words.txt file looks like this:
abate chicanery disseminate gainsay latent aberrant coagulate dissolution garrulous laud
It has 334 total words in it. I'm trying to display the list until it reaches a word with a length of 4 and stops.
wordsFile = open("words.txt", 'r')
words = wordsFile.read()
wordsFile.close()
wordList = words.split()
#List outputs length of words in list
lengths= [len(i) for i in wordList]
for i in range(10):
if i >= len(lengths):
break
print(lengths[i], end = ' ')
# While loop displays names based on length of words in list
while words != 4:
if words in wordList:
print("
Selected words are:", words)
break
output
5 9 11 7 6 8 9 11 9 4
sample desired output
Selected words are:
Abate
Chicanery
disseminate
gainsay
latent
aberrant
coagulate
dissolution
garrulous
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…