The goal is to find the line in which a word occurs in a given .txt file.
In my code I’ve defined a function and have used with open syntax.When I execute it,all i get is “enter the word you want to find” and when i enter the word,the execution automatically ends.I’ve been cracking my head as to where I went wrong,but could’nt spot my mistake.I’d be greatful if you can spot the error in the above code.
The following code in python is what I’ve tried
def find_line(x):
f=open("practice.txt","r+")
a=f.read()
b=f.readlines()
if(a.find(x)==-1):
print("word not found!")
else:
line_number=1
for line in b:
if(line.find(x)==-1):
line_number+=1
else:
print("the word exists in the line,whose number is",line_number)
x=input("enter the word you want to find")
find_line(x)
It stopped execution after the word input.
Arjun is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.