Reading Time: 1 minutes
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 |