Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Q:Write Python code that reads text from a provided file, searches for a specific word in the text, and returns the position ( line number

Q:Write Python code that reads text from a provided file, searches for a specific word in the text, and returns the position (line number) of the first occurrence of this word.
Instructions:
1. Open and read the contents of the file sample.txt ( given below) as example
2. Iterate through each line of the text to find the first occurrence of the word. Remember to keep track of the line number.
3. Once the word is found, return the line number to the user.
4. If the word is not found after iterating through all lines, inform the user that the word does not exist in the text by returning "Does not exist".
Example:
Sample.txt
Hello and welcome to the Python exercise.
This text contains multiple lines and words for you to search. Try finding different words to see how effective your function is. Remember the position should reflect the line number of the text that you want to search. Good luck with your Python coding journey.
If the word to be searched is "Python", the expected output should be "Line 1". Write a function find_word() and make sure the output "e.g. Line 1" is returned using the function.
Hint: You may start with the code below to first read the text from the file as separate lines.
with open("sample.txt") as file:
text= file.read()
lines= text.split("
")
In this exercise, it's recommended to use the 'split function. The 'split' function is used to split a string into a list of substrings based on a specified delimiter. When you call split() on a string, you provide the delimiter as an argument, and Python returns a list of substrings obtained by splitting the original string wherever the delimiter occurs.
For example:
text "apple banana orange"
fruits text.split("") # splits by space
fruits is a list with 3 items ["apple", "banana", "orange"]
text "apole.banana.orange"
ANS: def find_word(word):
with open("sample.txt") as file:
text = file.read()
lines = text.split("
") # Split by newline character to preserve line breaks
line_number =1 # Start line numbering from 1
for line in lines:
if word in line:
return f"Line {line_number}"
line_number +=1
return "Does not exist"
# Tests
print("Find Word:")
print(find_word("Python"))
print(find_word("position"))
print(find_word("computer"))
Can you fix and tell why this code is not working. And make it workable code

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Time Series Databases New Ways To Store And Access Data

Authors: Ted Dunning, Ellen Friedman

1st Edition

1491914726, 978-1491914724

More Books

Students also viewed these Databases questions

Question

Why is the System Build Process an iterative process?

Answered: 1 week ago