Hello,

I am trying to write a program that flips a coin until it gets the sequence "heads tails heads" and then stops and says how many flips it took to get that sequence. This is what I have so far:

```
import random
import time
import string
def coinflip():
heads = 0
tails = 0
flips = 0
hth = "HTH"
rec = open('cointoss.txt', 'w')
N = int(raw_input('Welcome to the Coin Toss! How many times do you want to flip the coin?: '))
while (flips < N):
flip = random.randint(1,2)
if flip == 1:
heads += 1
if heads > 0:
rec.write('H')
time.sleep(.1)
file = open('cointoss.txt', 'r')
text = file.read()
file.close()
search = text.find(hth)
if search == -1:
flips += 1
else:
flips += 1
print "needed", flips ," flips to get hth."
flips = N
else:
tails += 1
if tails > 0:
rec.write('T')
time.sleep(.1)
file = open('cointoss.txt', 'r')
text = file.read()
file.close()
search = text.find(hth)
if search == -1:
flips += 1
else:
flips += 1
print "needed", flips ," flips to get hth."
flips = N
print "you got", heads ," heads and", tails ," tails."
```

The way the code is, it runs to for the full N every time regardless of whether the sequence appears before it runs the full number of times. Why doesn't it stop when the HTH sequence appears? Any help is greatly appreciated.