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!

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

You're asking the user for their input twice, as to how many times to flip the coin. That's your first problem.

In this main program you're using trials as your count but then passing N to the function htthth.

All told, on each iteration of your while loop you're running coinflip.htthth() three times... Well actually both conditions check for the return to be equal to '1', which doesn't make much sense. Why is that? (I guess it makes sense as to why you're incrementing x by 2 each time) If you're trying to evaluate the return of that function you should do something like this:

rtn = coinflip.htthth(N)
if rtn == 1:
    # This is the action for a return of 1
elif rtn == 0: # Something OTHER THAN 1
    # This is the action for a return of 0

I hope that makes sense. Right now you're calling the function coinflip.htthth and passing it either 'hthCount' or 'httCount', which are strings. That function wants a number.

If the function does the looping for you, then you do not need to use a while loop to call it repeatedly. This is redundant.

Those are my initial observations, I hope they've helped. By the way, what class is this for?

This is for an intro to computer science class. What I am trying to do is have N be the maximum number of flips that occur in one trial, and when coinflip achieves either htt or hth within N, trial one is complete, and so on. The x incrementing by 2 was a typo I left in by accident to see if that would fix the issue, it should be x += 1.

This is for an intro to computer science class. What I am trying to do is have N be the maximum number of flips that occur in one trial, and when coinflip achieves either htt or hth within N, trial one is complete, and so on. The x incrementing by 2 was a typo I left in by accident to see if that would fix the issue, it should be x += 1.

Is this at Penn State? I feel like I've tried this assignment in the past, only in a different language...

Have you implemented any of the changes I mentioned above? How goes your progress? Do you understand my points, or do you need further explanation?

EDIT: Oh wow, I also just noticed that you have two return statements in your module's function... that is incorrect, as once the first return statement is encountered the function will exit immediately. The second return statement never happens.

I've modified you code by adding a function to do the "HTH" and "HTT" search. Since it is basically the same for 'H' and "T', one function can do it instead of two blocks of the same code . Note that the if heads/tails > 0 appears to be worthless, since you add one to the variable on the previous line. There is one "thought group" per function. One function to flip the coin, etc., and one function to test for the string to end it.

import random
import time

def find_it(text):
   htt = "HTT"
   hth = "HTH"
   time.sleep(.1)
   if hth in text:
      print "found HTH first", text
      return True
   elif htt in text:
      print "found HTT first", text
      return True

   ##  neither found
   return False
 
def htthth(N):
    
    heads = 0
    tails = 0
    flips = 0
    text = ""
    hthCount = 0
    httCount = 0
    found = 0

    while (flips < N):
        print flips
        flips += 1
        flip = random.randint(1,2)
        if flip == 1:
            heads += 1
##            heads will always be > 0 since you 
##            just added one to it
##            if heads > 0:
            
            text += 'H'
            found = find_it(text)
            if found:
               hthCount = 1
               flips = N
        else:
            tails += 1
            text += 'T'
            found = find_it(text)
            if found:
               httCount = 1
               flips = N
            
htthth(10)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.