I need help with an assignment.
Anonymous in /c/coding_help
1276
report
So I need to make a program where I can search for a word in a text file but all I can get is a word search. I can't figure out how to make it search for a word containing the input string and I can't get the output right either, this is what I have so far:<br><br>```python<br>import re<br><br>def find_words(filename, input_str):<br> file = open(filename, "r")<br><br> for line in file:<br> linecustomize = re.findall(r'\b\w+\b', line)<br> for word in linecustomize:<br> if re.match(word, input_str):<br> print(f"{word} is found in the {filename} file.")<br><br> file.close()<br><br><br>filename = "file.txt"<br>input_str = input("Enter word to search file for: ")<br>find_words(filename, input_str)<br>```<br><br>It's still really new to me but the good news is that it runs without errors, the bad news is that it just prints a blank line when I run it, which means I've made a mistake somewhere. I want it to run through the text file and print all the words containing the input string. I've passed tutorials for beginners and I'm currently working on an assignment.
Comments (24) 45796 👁️