Home > Blockchain >  Issue when going thru a .txt file looking for a word
Issue when going thru a .txt file looking for a word

Time:10-26

When the user inputs the word that they want to see, and what line its in. So the code is going to tell you what line its in.


userAns = input("Enter english word: ")
print("I will try to find that word now!\n\n")
found = False
count = 0

with open("english3.txt", "r ") as f:
    for line in f:
      count  = 1
      if userAns == line:
        print(f"I found it! in line {count}\n") 
        found = True
        break
      else:
        continue 
    if not found:
        print("I did not find it!\n")

print("I looked in a 1.8 MB file also")
print("I will have a larger file soon too!")
print("The code may get some thngs wrong")

The first line of the file is "a" so when you enter "a". It should say "found in line 1" or any other word that is within the file

any help would be nice

NOTE If you type "one" into it, it would say line 33, when its not line 33. The line CONTAINS the word and is not the actual word.

My code has that protection, and the if userAns == line is to help

CodePudding user response:

you need to read() the data in the file and split() by \n (lines) then go through each line and check if the word you are searching is in that line

userAns = input("Enter english word: ")
count = 0
with open("help.py", "r") as f:
    lines = f.read().split("\n")
    for line in lines:
        if userAns in line:
            print(f"I found it! in line {count   1}")
            break
        count  = 1
    else:
        print("I didn't found it!")

CodePudding user response:

this solution works for me:

userAns = input("Enter english word: ")
print("I will try to find that word now!\n\n")
with open("english3.txt", "r ") as f:
    found = False
    count = 0
    for line in f:
        count  =1
        if userAns in line:
            print(f"I found it! in line {count}\n")
            found = True
            break
        else:
            continue 
    if not found:
        print("I did not find it!\n")

CodePudding user response:

great attempt! Here's a few suggestions:

  1. No need for the continue in your logic

You don't need the else continue clause since this is the last statement in the for loop. The logic was going to go to the top of the for loop again without the continue.

  1. You can use a for / else statement.

The else clause of a for loop is used when the for loop terminates by running out of items. When a break gets executed, the else: clause of the for loop does not. You can simplify your logic a little by removing the found variable if you use the else clause.

  1. Use 'in' instead of '==' (depending on your requirements):

The == will only work if userAns is exactly the same as line. Incidentally, we should also consider new lines. The line variable may have a newline (\n) at the end whereas the user input userAns may not. Something to consider although it may not be an issue. The short of it is that you should consider removing trailing whitespace from your strings in this exercise with rstrip() string function.

I'll put this together in a subsequent edit to this response. However, I wanted to give you some pointers first. The most important pointer is 3. above, where the comparison will not work if you are not matching entire lines or you are getting defeated by newlines ('\n').

Here's the code with the modifications suggested above:

userAns = input("Enter english word: ")
print("I will try to find that word now!\n\n")
count = 0

with open("english3.txt", "r ") as f:
    for line in f:
      count  = 1
      if userAns in line.rstrip():
        print(f"I found it! in line {count}\n") 
        break
    else:
        print("I did not find it!\n")

print("I looked in a 1.8 MB file also")
print("I will have a larger file soon too!")
print("The code may get some thngs wrong")

Here's one more edit to the code above: you don't need to count the lines in the file - the enumerate() function can do that for you. Enumerate returns the current index and the current item as it goes through an iterable (or in this case, a file) for you.

Here's the code with the counting done by enumerate (although the code is not much simpler, it teaches you something useful):

userAns = input("Enter english word: ")
print("I will try to find that word now!\n\n")

with open("english3.txt", "r ") as f:
    for count, line in enumerate(f):
      if userAns in line.rstrip():
        print(f"I found it! in line {count 1}\n") 
        break
    else:
        print("I did not find it!\n")

print("I looked in a 1.8 MB file also")
print("I will have a larger file soon too!")
print("The code may get some thngs wrong")

Final Update:

As per Wolfie's requirements, they want to match the line exactly. So we'll need to use == instead of in, but we'll still need to deal with white spaces (or \n) at the end of the line. So here's one more code modification to meet these requirements:

userAns = input("Enter english word: ")
print("I will try to find that word now!\n\n")

with open("english3.txt", "r ") as f:
    for count, line in enumerate(f):
      if userAns == line.rstrip():
        print(f"I found it! in line {count 1}\n") 
        break
    else:
        print("I did not find it!\n")

print("I looked in a 1.8 MB file also")
print("I will have a larger file soon too!")
print("The code may get some thngs wrong")

Incidentally, there's a shell equivalent (sort of).

grep -n "^one$" english3.txt 

The above will tell you the line that one matches (completely).

  • Related