# Python 2.7 pulled out a while loop with a single line if no match was found

Trying to implement Boyer Moore, bad character matching algorithm without looking at the answers. I want to exit this loop I wrote with just one line of "No match" if there was no pattern found, here is my code:

``````T = 'ATGGGTATGGCTCGGCTCGG'
p = 'ATCG'

skip = 0

while skip + len(p) < len(T):

string = T[skip:skip+len(p)]

if string == p:
print "offset at: ", skip
break

for i in range(len(string), 0, -1):

if (string[i-1] != p[i-1]):

if string[i-1] in p[:i-1]:
skip += (i - p.find(string[i-1]) - 1)
break

elif not string[i-1] in p[:i-1]:
skip += i
break
```
```

Any hits regarding how to modify the code.

Thank you,

xp

edit: Scheme gave me the answer, as easy as that. I was so looping my head in the line string == p or string != p. Thank you for all your comments.

If i am understanding you correctly you would like to exit your while loop and print "no match" only once, if the string p is not contained in t. This looks like the all too common DNA exercise.

Anyway a simple way to do what you want is to check for the substring p in t and break if it is not found. The answer here: How to determine whether a substring is in a different string would help with that.

Basically:

``````T = 'ATGGGTATGGCTCGGCTCGG'
p = 'ATCG'

skip = 0

while skip + len(p) < len(T):

if not p in T:
print "no match"
break

string = T[skip:skip+len(p)]

if string == p:
print "offset at: ", skip
break

for i in range(len(string), 0, -1):

if (string[i-1] != p[i-1]):

if string[i-1] in p[:i-1]:
skip += (i - p.find(string[i-1]) - 1)
break

elif not string[i-1] in p[:i-1]:
skip += i
break
```
```

The key here is the line `if not p in T:` Which is basically saying If the string p does not exist inside the string T then do something. else, carry on.

On top of this you may want to consider looking at some good practices like variable naming, and using functions.