[Tutor] Coin game
bob gailer
bgailer at gmail.com
Tue Nov 29 05:31:07 CET 2011
On 11/27/2011 7:43 PM, Guess?!? wrote:
>
> Two players take turns flipping a fair coin. The game ends when the
> same outcome occurs on three flips in a row. Whichever player flipped
> the coin last, wins. For example:
>
>
Here's my version of a simple compact program.
import random
N = 10
tosses = ''.join(random.choice("HT") for i in range (N))
w = min(tosses.find("TTT") % N + 1,tosses.find("HHH") % N + 1)
if w == N: print 'no winner'
else: print tosses[:w+2], 'player %s wins.' %((w % 2) + 1,)
--
Bob Gailer
919-636-4239
Chapel Hill NC
More information about the Tutor
mailing list