2

Im still new to python so ill try my best to explaining what I have done So basically my code asks the user for input it then imports a text file which contains words and each word is in a separate line, my code then store the users input in a separate file and only uses the first line of the user input I am trying to see if the words that my user input exists in the pre set text file I imported and then in my full code I would do some operations on that words but its not working i have tried using "==" and using "counter" and "if x in y...." and also if the user input does exist in text file the count on the last line prints zero.

user_input=input("Enter: ")
user_input=user_input.upper()
user_input=user_input.split()
print(user_input)
list_words=[]
with open("words.txt","r") as words:
    english_words=words.readlines()
    for line in english_words:
       line=line.upper()
       list_words.append(line.rstrip('\n'))
with open("user-message.txt","w+") as file_of_user_inp:
       for word in user_input:
           file_of_user_inp.write(word +"\n")
           
with open("user-message.txt","r") as file_of_user_inp:
       first_line_user_inp=file_of_user_inp.readline()
       user_input=first_line_user_inp

for each_eng_word in list_words:
    for each_word in user_input:
        if each_word==each_eng_word:
            print("it worked")

print(list_words.count((user_input)))
Salman
  • 21
  • 1
  • It isn't clear what you you are getting v. what you are expecting. – Tom Myddeltyn Dec 10 '20 at 14:49
  • 1
    Saving the user input to a file seems like an unnecessary step. Is that required? – 001 Dec 10 '20 at 14:51
  • 1
    You don't strip the newline when you read back the user input. – 001 Dec 10 '20 at 14:55
  • A couple suggestions for speeding up your code. 1) break when you find a match (if the first one matches, you still compare the remaining `n-1` elements. 2) you could simplify by using `list.index()` to search or `if word in list:` You could also make them `set()` and do set operations. Also, I'm not sure if you want to iterate through the large set of words in your outer loop Do you want to make sure that every word in your 'words.txt' is in the input or the other way around? – Tom Myddeltyn Dec 10 '20 at 15:00
  • @JohnnyMopp unfortunately it is required for me to save it in a file after I save it into a file I just want to test the first line (which is a single word) I have an if statement in my full code where I then import the full file and carry on operations with the file, user input – Salman Dec 10 '20 at 15:15
  • @JohnnyMopp What do you mean by strip new line – Salman Dec 10 '20 at 15:15
  • @TomMyddeltyn Yes in my full code I break once I find a match and I am using the if function but I was editing this part of code on a separate template – Salman Dec 10 '20 at 15:19
  • 1
    `first_line_user_inp=file_of_user_inp.readline()` The resulting string will have `\n` at the end. Remove it like you did with the word file with `rstrip('\n')` – 001 Dec 10 '20 at 15:21
  • @JohnnyMopp It worked! I've been trying for hours thanks! – Salman Dec 10 '20 at 15:39

1 Answers1

0

Here's a program I made that might be able to help. You can type a list of words separated by spaces and it will check a text document for any matches. If it finds a match it will print the matching word(s).

The First thing I did was create variables the user_input, list1 which holds all of the words from your text document, numbers, WordString which is a temporary string that holds your words before they are added to the list.

user_input = input("Enter: ")
list1 = []
numbers = "1234567890"
WordString = ""

Here I open a text document and use the read() function to read the text document.

with open("YOUR TEXT DOCUMENT", "r") as b:
    words = b.read()

Here I loop through every character inside your text document using a "for loop", "range()", and "len()".

The first "if conditional" checks if the xth letter in the text document is not a space and checks if the xth letter is not a number (or not in our "number variable" which holds a string of numbers anyway.) This allows all consecutive letters to be counted as words. We add these letters in order to the "WordString" string.

The second "if conditional" checks if the word has ended or not. It does this by checking if you have reached a space or a number or the end of the text document. If it has met any of these conditions it will add your finished word to the list1 array. The "-1" is because len() measures characters starting at one and items inside arrays start counting at 0. Once we find a complete word we reset WordString back to a empty string so it is ready to go through the loop again.

for x in range(len(words)):
    if words[x] != " " and words[x] not in numbers:
        WordString += words[x]
    if words[x] == " " or words[x] in numbers or x == len(words) - 1:
        list1.append(WordString)
        WordString = ""

This second loop also checks for words, but it's looking for the words that you typed in the "user_input" variable instead. It also reuses the "WordString" variable from before.

The only difference lies in the second if statement. Once we find a complete, finished word. We check to see if it is a word that is inside of the list1 array (the place where we put all the words from the text document.). If if it is infact a word inside of the list1 array we print it.

for y in range(len(user_input)):
    if user_input[y] != " " and user_input[y] not in numbers:
        WordString += user_input[y]
    if user_input[y] == " " or user_input[y] in numbers or y == len(user_input) - 1:
        if WordString in list1:
            print(WordString)
        WordString = ""

I tried to explain it best I could, I'm a beginner with Python also. There may be a more efficient way and I'm sure other veterans can explain it better. But here's my input (no pun intended).