Files
lab_dice/dice_stats.py
mollychi b52f578504 I knew I had to see how many of the dice were the same but I unsure how to do it so i did some research on the internet to help me solve this one.
A problem you could solve with classes in coding could be a algebara problem that requires multiple steps. I have trouble thinking of real life situtations
2025-11-09 20:10:19 -05:00

51 lines
1.0 KiB
Python

from die import Die
from tqdm import tqdm
from collections import Counter
class FiveDice:
def __init__(self):
self.dice = [Die() for number in range(5)]
def roll(self):
for die in self.dice:
die.roll()
return self.faces()
def faces(self):
return [die.face for die in self.dice]
def all_ones(self):
for face in self.faces():
if face != 1:
return False
return True
def is_three_of_a_kind(self):
counts = Counter(self.dice)
for count in counts.value():
if count >= 3:
return True
return False
def is_four_of_a_kind(self):
counts = Counter(self.dice)
for count in counts.value():
if count >= 4:
return True
return False
dice = FiveDice()
successes = 0
trials = 1000000
for trial in tqdm(range(trials)):
dice.roll()
if dice.all_ones():
successes += 1
print(successes/trials)