0

I'm making a script that reads a dictionary and picks out words that fit a search criteria. The code runs fine, but the problem is that it doesn't write any words to the file "wow" or print them out. The source for the dictionary is https://github.com/dwyl/english-words/blob/master/words.zip.

I've tried changing the opening of the file to "w+" instead of "a+" but it didn't make a difference. I checked if there just weren't any words that fitted the criteria but that isn't the issue.

listExample = [] #creates a list with open("words.txt") as f: #opens the "words" text file for line in f: listExample.append(line) x = 0 file = open("wow.txt","a+") #opens "wow" so I can save the right words to it while True: if x < 5000: # limits the search because I don't want to wait too long if len(listExample[x]) == 11: #this loop iterates through all words word = listExample[x] #if the words is 11 letters long lastLetter = word[10] print(x) if lastLetter == "t": #and the last letter is t file.write(word) #it writes the word to the file "wow" print("This word is cool!",word) #and prints it else: print(word) #or it just prints it x += 1 #iteration else: file.close() break #breaks after 5000 to keep it short 

It created the "wow" file but it is empty. How can I fix this issue?

6
  • 1
    You sure the if condition is met to execute file.write(word) Commented Jul 11, 2019 at 11:02
  • 1
    Are you sure all the conditions that lead to writing to the file are met? Commented Jul 11, 2019 at 11:02
  • The conditions are that it is a 11 letter word with the letter t at the end. "adolescent" is in the dictionary and is not written to file. Commented Jul 11, 2019 at 11:04
  • @ix84gaming, there are 10 letters in this word, not 11 Commented Jul 11, 2019 at 11:05
  • len("adolescent") gives me 10 Commented Jul 11, 2019 at 11:06

1 Answer 1

2

This fixes your problem. You were splitting the text in such a way that each word had a line break at the end and maybe a space too. I've put in .strip() to get rid of any whitespace. Also I've defined lastLetter as word[-1] to get the final letter regardless of the word's length.

P.S. Thanks to Ocaso Protal for suggesting strip instead of replace.

listExample = [] #creates a list with open("words.txt") as f: #opens the "words" text file for line in f: listExample.append(line) x = 0 file = open("wow.txt","a+") #opens "wow" so I can save the right words to it while True: if x < 5000: # limits the search because I don't want to wait too long word = listExample[x].strip() if len(word) == 11: lastLetter = word[-1] print(x) if lastLetter == "t": #and the last letter is t file.write(word + '\n') #it writes the word to the file "wow" print("This word is cool!",word) #and prints it else: print(word) #or it just prints it x += 1 #iteration else: print('closing') file.close() break #breaks after 5000 to keep it short 
Sign up to request clarification or add additional context in comments.

1 Comment

And one more thing ;) The first loop over words.txt can be simplified by using either list or readlines: docs.python.org/3/tutorial/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.