Python @ DjangoSpin

PyPro #79 Counting Heads & Tails in 100 tosses of a coin

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon
Reading Time: 1 minutes

Counting Heads & Tails in 100 tosses of a coin in Python

Counting Heads & Tails in 100 tosses of a coin in Python

Write a program for counting heads & tails in 100 tosses of a coin.

Counting Heads & Tails in 100 tosses of a coin

import random
   
tally = {'heads': 0, 'tails': 0
 
outcomes = list(tally.keys())               # ['heads', 'tails']
   
for nthCoinToss in range(100):
    tally[random.choice(outcomes)] += 1
   
print('Heads: ', tally['heads'])            # Heads:  46
print('Tails: ', tally['tails'])            # Tails:  54

See also:

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon

Leave a Reply