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

2

u/citizen_lost May 21 '20

Really interesting to see how a different IDE works.

Is that OS windows? Doesn't look like it!?

2

u/okaydexter May 22 '20

it is Ubuntu 18.04

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]}")