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 am not sure I understood the question but is this what you want?. this will take the line containing the most words from the user input:</p>

<pre><code>problem = a.split(' ')
max_num, current_num = 0,0  #max_num count the maximum apparition of words from the input in the line| current_num count the current number of apparition of words of the user input in the line
chosen_line = ''

with open('solutions.txt', 'r') as searchfile:
    for line in searchfile:
        for word in problem:
            if word in line:
                current_num+=1
        if current_num&gt;max_num:
            print line,max_num,current_num
            max_num=current_num
            chosen_line = line
        current_num = 0
    print chosen_line
</code></pre>

<p>but it seems to me the easiest way to do what you want is to store at the start of each answer the question, or even easier - just ask the user the question number and return this corresponding answer.</p>

python python-3.x