I'm trying to write a coin-flipping program.
I can get the program to produce a random coin flip result (represented as either "h" or "t") but I want to add the individual result to an ever expanding string.
For example, the first result would be "h"
and the big string would be : h
The second flip would be "t"
and the big string would CHANGE to: ht
But, I can figure out how to add the strings together.
here's my code:
import random numflips = 1 def flip(): cheads, ctails, = 0,0 cflips = random.randrange(2) if cflips == 0: cheads += 1 return 'h' else: ctails += 1 return 't' numflips +=1 while flip() == 't' or 'h':
when the sequence "hth" appears, I want the program to stop and tell me how many flips it took to get there. With this, I am totally lost. Does Python have a "find" command :-P?
Thanks for any help.