Basic coin flipper program - logical error help

John Zenger john_zenger at yahoo.com
Tue Feb 21 21:06:54 EST 2006


wes weston wrote:
>    Looping is easier with:
> for x in range(100):
>    if random.randint(0,1) == 0:
>       heads += 1
>    else:
>       tails += 1

Also, with the functional programming tools of map, filter, and lambda, 
this code can be reduced to just six lines:

import random

flips = map(lambda x: random.randrange(2), xrange(100))
heads = len(filter(lambda x: x is 0, flips))
tails = len(filter(lambda x: x is not 0, flips))

print "The coin landed on heads", heads, "times."
print "The coin landed on tails", tails, "times."



More information about the Python-list mailing list