Newbie Program
Daniel Schüle
uval at rz.uni-karlsruhe.de
Fri Aug 5 08:06:59 EDT 2005
Eric schrieb:
> I am reading a book on Python and ran across and exercise that I just
> can't seem to figure out. Its pretty simple, but I just can't get
> past a certain point.
>
> The task is to create a program that flips a coin 100 times and keeps
> track of the total of heads and tails which is printed to the screen.
>
> My plan was to use import random with a range of 2 and use a
> conditional statement to count to 100. The problem I am encountering
> is how do I keep track of the totals for heads and tails?
>
> Any suggestions would be appreciated. Keep in mind I am very new to
> Python so there may be a more sophisticated way, but I am just trying
> to use what the book has taught so far.
>
> Thanks,
> Eric
>>> import random
>>> coin = ("head", "tail")
>>> cnt = {"head":0, "tail":0}
>>> for i in xrange(100):
... cnt[random.choice(coin)] += 1
...
>>> cnt
{'head': 49, 'tail': 51}
>>>
my solution
More information about the Python-list
mailing list