else statement keep looping print ‘not found’ in python [duplicate]

Are you looking for the break statement in python? As the name reflects, this statement simply breaks you out of the loop.

Eg:

for line in f.readlines():
        if line.find('chocolate') != -1:
            print "found ", line

        elif line.find('milkshake') != -1:
            print "found ", line

        else:
            print "not found"
            break

Leave a Comment