r/inventwithpython • • May 21 '20

Casual Programming With Python & Music : Determine Heads & Tails When A Coin Is Flipped 1000 Times

https://youtu.be/iLlEpqHuYYE

[removed] — view removed post

8 Upvotes

3 comments sorted by

View all comments

2

u/masasin May 22 '20 edited May 22 '20

Why'd you import itertools?

Bonus: Python 3.6+ allows you to use random.choices(sides, k=1000).

import random

sides = ["heads", "tails"]
flips = random.choices(sides, k=1000)

for side in sides:
    print(f"{side}: {flips.count(side}")

You could also use collections.Counter to count for you instead of running through the whole thing every time, which might be useful if you have many more options.

from collections import Counter
import random

sides = range(1, 7)
counts = Counter(random.choices(sides, k=1000))

for side in sides:
    print(f"{side}: {counts[side]}")