#!/usr/bin/python # simulate coin game where first person to flip heads wins # print one game per line from random import choice while True: # do it forever! flip = None flips = '' while flip != 'H': flip = choice(['H', 'T']) # choose H or T with equal probability flips += flip # append flip to the end of the string # Print the winning player and the sequence of tosses. The # 'try/except' clause is so that Python doesn't complain about a # broken pipe when the script is piped to 'head'. try: print 1+(len(flips+'H') % 2), flips except IOError: break