created new function to return a count of each face value in a roll of 5 dice.

I think it could be made to be cleaner by calling a function that determines
how many "of a kind" there are by passing arguments of how many "of a kind"
you are looking for.  2 of a kind, 3 of a kind, 4 of a kind etc.
This commit is contained in:
Hope 2025-07-23 20:55:43 -04:00
parent 81ad4a3355
commit b5acf78bbc
1 changed files with 50 additions and 28 deletions

View File

@ -12,46 +12,67 @@ class FiveDice:
def faces(self):
return [die.face for die in self.dice]
def value_count(self):
#will count how many of each kind of dice and return a list of how many of each value
count_ones = 0
count_twos = 0
count_threes = 0
count_fours = 0
count_fives = 0
count_sixes = 0
for face in self.faces():
if face == 1:
count_ones += 1
if face == 2:
count_twos += 1
if face == 3:
count_threes += 1
if face == 4:
count_fours += 1
if face == 5:
count_fives += 1
if face == 6:
count_sixes += 1
counts = {
"ones": count_ones,
"twos": count_twos,
"threes": count_threes,
"fours": count_fours,
"fives": count_fives,
"sixes": count_sixes}
# print (counts)
return counts
def all_ones(self):
for face in self.faces():
if face != 1:
value_count = self.value_count()
#print (value_count)
#print(value_count["ones"])
if value_count["ones"] < 5:
return False
return True
def is_three_of_a_kind(self):
#set current roll of 5 dice to a list variable dice
dice = self.faces()
for die in range(len(dice)):
#take each roll value and compare it to the other values.
face_value = (dice[die])
how_many = 0
for d in range(len(dice)):
#if the roll value is the same as another value, increase the count of how many
if die == face_value:
how_many +=1
#if the cound of how many of that value is 3 or more, then you got three of a kind. So, return true.
if how_many >= 3:
value_count = None
value_count = self.value_count()
for value in value_count.values():
if value > 3:
return True
return False
else:
return False
def is_four_of_a_kind(self):
#set current roll of 5 dice to a list variable dice
dice = self.faces()
for die in range(len(dice)):
#take each roll value and compare it to the other values.
face_value = (dice[die])
how_many = 0
for d in range(len(dice)):
#if the roll value is the same as another value, increase the count of how many
if die == face_value:
how_many +=1
#if the count of how many of that value is 4 or more, then you got three of a kind. So, return true.
if how_many >= 4:
value_count = None
value_count = self.value_count()
for value in value_count.values():
if value > 4:
return True
return False
else:
return False
dice = FiveDice()
@ -71,6 +92,7 @@ for trial in tqdm(range(trials_3)):
dice.roll()
if dice.is_three_of_a_kind():
successes_3 += 1
print(f"Three of a Kind: {successes_3/trials_3}")