Home > Blockchain >  word Isn't equal to wordlist.readline() even though it is
word Isn't equal to wordlist.readline() even though it is

Time:12-30

Here's my code:

wordlist = open('words.txt')

word = input()
i = 0

def loop():
    for i in range(466549):
        if wordlist.readline().strip() == word:
            print(f'found in {i}')
            return
        else:
            print(f"not {wordlist.readline()}")
            i = i   1
            
loop()

Not only does it not return the first value of the list, but it also doesn't say that it found my word when it did. I got my words.txt file from GitHub, and the second word is "1080". But when I put that in, it prints 'not 1080' even though it clearly is.

CodePudding user response:

I think what you are looking for is:

word = input("enter word: ").strip()

def loop():
    with open("words.txt") as f:
        wordList = f.readlines()
        for i,line in enumerate(wordList):
            line = line.strip()
            if line == word:
                print(f"found in line {i}")
                return
            else:
                print(f"did not find in {line}")


loop()

or if you want to stick to using just readline:

word = input("enter word: ").strip()
wordList = open("words.txt")

def loop():
   for i in range(466549):
       line = wordList.readline().strip()
       if line == word:
          print(f"found in line {i}")
          return
       else:
          print(f"did not find in {line}")


loop()
wordList.close()

CodePudding user response:

Maybe you wanna find if specific word in text file.

First, wordlist.readline() actually reads a line from file which means it is not reproducible. so, print(f"not {wordlist.readline()}") doesn't work as you expected.

For example, let's say words.txt looks like below.

hello
1080
world

When you read first line with if wordlist.readline() ... inside loop, that value is "hello", and this is not 1080, so else statement runs. At here, you reads a line with print(f"not {wordlist.readline()}") as second which is 1080, thus not 1080 gonna be printed.

And finally, if wordlist.readline() ... will read world from file, this is also not 1080, and next readline() will be None.

Secondary problem is that variable i is not utilized properly. i is incremented automatically thanks to range and for keyword (Strictly, it just next value from iterable object, not increment), so you don't need like i = 0 or i = i 1

Third thing is derived from the problem pointed in first one, which is None.strip() is impossible so it will raise an exception.

There are more things to say, but I'll just show you my example so you can compare it with yours

input_wordlist = open('words.txt')
input_word = input()

def loop(word, wordlist):
    for index, line in enumerate(wordlist.readlines()):
        if line.strip() == word:
        # or if you wanna know whether word is in line
        # if word in line:
            print(f"found in {index}")
            return
        else:
            print(f"not in {line}")

loop(input_word, input_wordlist)
input_wordlist.close()
  • Related