I can't get the math to work in this program. The 'coinflip' flips a coin until it finds either the sequence heads-tails-tails or heads-tails-heads. My issue is that when I run it, the program will perform way more trials than told, and will only account for a random few in the htt and hth sums; for example, if the input for trials is 5, it runs the loop 14 times and tells me it 4 HTH's and 1 HTT. This is the program:
import coinflip
x = 0
hth = 0
htt = 0
N = int(raw_input('Welcome to the HTH-HTT Head-to-Head! How many times do you want to flip the coin per trial? I recommend at least 20: '))
trials = int(raw_input('How many trials do you want to run?: '))
while x < trials:
coinflip.htthth(N)
if coinflip.htthth('hthCount') == 1:
hth += 1
x += 2
elif coinflip.htthth('httCount') == 1:
htt += 1
x += 2
if htt > hth:
print "You got HTH", hth ,"times and HTT", htt ," HTT times. So I'd say HTT is more likely."
elif hth > htt:
print "You got HTH", hth ,"times and HTT", htt ," HTT times. So I'd say HTH is more likely."
else:
print "It appears that HTH and HTT are equally likely."
and this is the module:
def htthth(N):
heads = 0
tails = 0
flips = 0
htt = "HTT"
hth = "HTH"
text = ""
hthCount = 0
httCount = 0
while (flips < N):
flip = random.randint(1,2)
if flip == 1:
heads += 1
if heads > 0:
text += 'H'
time.sleep(.1)
hthSearch = text.find(hth)
httSearch = text.find(htt)
if hthSearch != -1:
print "found HTH first."
hthCount = 1
flips = N
elif httSearch != -1:
print "found HTT first."
httCount = 1
flips = N
else:
tails += 1
if tails > 0:
text += 'T'
time.sleep(.1)
hthSearch = text.find(hth)
httSearch = text.find(htt)
if hthSearch != -1:
print "found HTH first."
hthCount = 1
flips = N
elif httSearch != -1:
print "found HTT first."
httCount = 1
flips = N
print "your record was ", text ,"."
return hthCount
return httCount
I appreciate any help!