How do you look for a line in a text file, from a sentence a user has inputted, by using its keywords?
<pre><code>a=input("Please enter your problem?")
problem= ()

with open('solutions.txt', 'r') as searchfile:
    for line in searchfile:
        if problem in line:
            print (line)
</code></pre>

<p>Can someone please help me on how to get the keywords from the inputed string by the user. Thanks. I need help on how to look for some of the words the users inputed in to =a and search for them on the textfile and print the line</p>

<p>I assume your keywords is meant to be a list? </p>

<p>Then you use <a href="https://docs.python.org/3/library/functions.html#any" rel="nofollow"><code>any()</code></a> to check if any word out of the keywords is in the line. </p>

<pre><code>a=input("Please enter your problem?")
problem= ['#keywords', 'not', 'sure', 'how']

with open('solutions.txt', 'r') as searchfile:
    for line in searchfile:
        if any(word in line for word in problem):
            print (line)
</code></pre>

<p>Though, you may want to <code>split()</code> your line to improve that detection. </p>

<p>Otherwise, you have <code>a</code>, which stores the user's input, so you can use that. </p>

<pre><code>a=input("Please enter your problem?")
problem= a.split()
</code></pre>

<p>Then, again <code>problem</code> is a list, so you use <code>any()</code> as before</p>

<p>Or, if you want to check if the entire entered value is in a line, then </p>

<pre><code>if a in line:
    print(line)
</code></pre>

python python-3.x