Print "Match Found" for each line the search is in and "Match not Found" if the search isnt in the whole file
<p>I want to print "Match Found" and the line that the match is in if the search is in a line in the file. Note - the match can be in more than one line and if so it should print every line the match is in. I have got this part working but i want to print "Match Not Found" once if the search isn't in the whole file. So far whatever I have tried either prints "Match Not Found" for every line the search isnt in or it prints "Match not Found" after printing the lines the search is in.</p>

<pre><code>f = open("cars.txt", "r+")
search = input("What do you want to search for? ")
for line_split in f:
    if search in line_split:
        print ("You searched for %s." % search)
        print ("Match Found.")
        print (line_split)
        print ("--------------------")
else:
        print ("You searched for %s" % search)
        print ("Match Not Found.")
        print ("--------------------")
f.close
</code></pre>

<p>The <code>else</code> block of the <code>for</code> would have been a great choice if you had to <code>break</code> the iteration once an item is found; but that isn't the case.</p>

<p>I think you may want to set a <em>flag</em> instead of using the <code>else</code> and testing after the <code>for</code> is completed:</p>

<pre><code>found = None
for line_split in f:
    if search in line_split:
        ...
        if not found:
            found = True

if not found:
    print ("You searched for %s" % search)
    print ("Match Not Found.")
</code></pre>

python python-3.x