From 3d5c2e1d8c61f451719308c60c35098dfb03045f Mon Sep 17 00:00:00 2001 From: Hope Date: Wed, 16 Jul 2025 08:25:03 -0400 Subject: [PATCH] Updated the logic to have a three and four of a kind logic. I set the list to a variable because I kept getting an error saying the object was not iterable. --- dice_stats.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/dice_stats.py b/dice_stats.py index 83a99cb..af915f4 100644 --- a/dice_stats.py +++ b/dice_stats.py @@ -18,6 +18,41 @@ class FiveDice: if face != 1: 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: + return True + 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: + return True + return False + dice = FiveDice() successes = 0 @@ -27,7 +62,27 @@ for trial in tqdm(range(trials)): if dice.all_ones(): successes += 1 -print(successes/trials) +print(f"All Ones: {successes/trials}") + +dice = FiveDice() +successes = 0 +trials = 1000000 +for trial in tqdm(range(trials)): + dice.roll() + if dice.is_three_of_a_kind(): + successes += 1 + +print(f"Three of a Kind: {successes/trials}") + +dice = FiveDice() +successes = 0 +trials = 1000000 +for trial in tqdm(range(trials)): + dice.roll() + if dice.is_four_of_a_kind(): + successes += 1 + +print(f"Four of a Kind: {successes/trials}")