Hello,

I want to total up a variable that changes every time a function is run.

import random
def flip():
	h,t = 0,1
	cheads, ctails, = 0,0
	cflips = random.choice((0,1))
	if cflips == 0:
		cheads += 1
		return 'h'
	else:
		ctails += 1
		return 't'		
def repeat(f,n):
	for i in range(n):
		flip()
		results = ""
		while not results.endswith("hth"):
			results += flip()
		else:
			flips = len(results)
			print "it took" , flips , "to find HTH"
repeat(flip, 10000)

It's that flips variable towards the end. Every time the function executes, it changes. I want to run the flip() function 10,000 times and add up the flips variable 10,000 times as well. Usually, the number of flips is somewhere around 8-16, so my total should be somewhere around 80,000-160,000.

Thanks,

Is this what you are after?

If you change your repeat function to

def repeat(f, n):
    totalFlips = 0
    for i in range(n):
        results = ""
        while not results.endswith('hth'):
            results += f()
            totalFlips += 1
        print("It took {0} flips to find HTH".format(len(results)))
    return totalFlips

If you call this with repeat(flip, 10000) it will print out 10000 lines with "It took ..." and return the actual number of flips it took.

And by the way, the cheads and ctails in the flip-function resets for every flip. If you want to keep this, I would suggest to create a class for it.

class Flipper(object):
    def __init__(self):
        self.cheads, self.ctails = 0, 0
    
    def flip(self):
        flip = random.choice((0,1))
        if flip == 0:
            self.cheads += 1
            return 'h'
        else:
            self.ctails += 1
            return 't'

    def repeat(self, f, n):
        totalFlips = 0
        for i in range(n):
            results = ""
            while not results.endswith('hth'):
                results += f()
                totalFlips += 1
            print("It took {0} flips to find HTH".format(len(results)))
        return totalFlips

flipper = Flipper()
totalFlips = flipper.repeat(flipper.flip, 10000)

Alright,

I change it to run as a giant for loop with the function inside:

import random
for i in range(1,10000):
	def flip():
		h,t = 0,1
		cheads, ctails, = 0,0
		cflips = random.choice((0,1))
		if cflips == 0:
			cheads += 1
			return 'h'
		else:
			ctails += 1
			return 't'		
	results = ""
	while not results.endswith("hth"):
		results += flip()
	else:
		flips = len(results)
		print "it took" , flips , "to find HTH"

So, I need to extract "flips" from every cycle and add it to variable. Something like "totalFlips =(AGGREGATE)flips"

Sorry for the pseduocode.

-V

Hot diggity dog! Another tab artist, must be the teacher!
'vidaj' gave you the answer with his repeat() function, use it. All you need to do is to rewrite the test print to match the older version of Python.

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.