generated from mwc/lab_dice
wrote the methods for checking how many times a three of a kid or four of a kind was rolled
This commit is contained in:
@@ -13,12 +13,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:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def is_three_of_a_kind(self):
|
||||
#In the line below, use self.value_count so that it knows that value_count refers to the method in this instance of this class
|
||||
|
||||
counts = self.value_count()
|
||||
# because we need to refer to the values in the dict vs the dict item as a whole, we need to use the count.values() function to refer to the values in the dict.
|
||||
for count in counts.values():
|
||||
##logic question, this will show only exactly 3 of a kind. If you roll 4 of a kind would that be a success of three of a kind? If it would, how would you change this?
|
||||
if count >= 3:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def is_four_of_a_kind(self):
|
||||
#In the line below, use self.value_count so that it knows that value_count refers to the method in this instance of this class
|
||||
counts = self.value_count()
|
||||
for count in counts.values():
|
||||
if count == 4:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
|
||||
dice = FiveDice()
|
||||
successes = 0
|
||||
trials = 1000000
|
||||
@@ -27,7 +82,26 @@ for trial in tqdm(range(trials)):
|
||||
if dice.all_ones():
|
||||
successes += 1
|
||||
|
||||
print(successes/trials)
|
||||
print("Odds for rolling all ones: "+ str(successes/trials))
|
||||
|
||||
dice = FiveDice()
|
||||
successes_3 = 0
|
||||
trials = 1000000
|
||||
for trial in tqdm(range(trials)):
|
||||
dice.roll()
|
||||
if dice.is_three_of_a_kind():
|
||||
successes_3 += 1
|
||||
###If you want to modify this to make the print more clear about what you are printing you can concatinate a description string like I did for the all ones trial above.
|
||||
print("Odds for rolling all threes: "+ str(successes_3/trials))
|
||||
|
||||
dice = FiveDice()
|
||||
successes_4 = 0
|
||||
trials = 1000000
|
||||
for trial in tqdm(range(trials)):
|
||||
dice.roll()
|
||||
if dice.is_four_of_a_kind():
|
||||
successes_4 += 1
|
||||
|
||||
###Don't forget to call the print again here for the trial for 4 of a kind
|
||||
print("Odds for rolling all fours: "+ str(successes_4/trials))
|
||||
|
||||
|
||||
1
die.py
1
die.py
@@ -15,3 +15,4 @@ class Die:
|
||||
def roll(self):
|
||||
self.face = randint(1, 6)
|
||||
return self.face
|
||||
|
||||
|
||||
56
poetry.lock
generated
Normal file
56
poetry.lock
generated
Normal file
@@ -0,0 +1,56 @@
|
||||
# This file is automatically @generated by Poetry 2.3.1 and should not be changed by hand.
|
||||
|
||||
[[package]]
|
||||
name = "click"
|
||||
version = "8.3.1"
|
||||
description = "Composable command line interface toolkit"
|
||||
optional = false
|
||||
python-versions = ">=3.10"
|
||||
groups = ["main"]
|
||||
files = [
|
||||
{file = "click-8.3.1-py3-none-any.whl", hash = "sha256:981153a64e25f12d547d3426c367a4857371575ee7ad18df2a6183ab0545b2a6"},
|
||||
{file = "click-8.3.1.tar.gz", hash = "sha256:12ff4785d337a1bb490bb7e9c2b1ee5da3112e94a8622f26a6c77f5d2fc6842a"},
|
||||
]
|
||||
|
||||
[package.dependencies]
|
||||
colorama = {version = "*", markers = "platform_system == \"Windows\""}
|
||||
|
||||
[[package]]
|
||||
name = "colorama"
|
||||
version = "0.4.6"
|
||||
description = "Cross-platform colored terminal text."
|
||||
optional = false
|
||||
python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7"
|
||||
groups = ["main"]
|
||||
markers = "platform_system == \"Windows\""
|
||||
files = [
|
||||
{file = "colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6"},
|
||||
{file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"},
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tqdm"
|
||||
version = "4.67.1"
|
||||
description = "Fast, Extensible Progress Meter"
|
||||
optional = false
|
||||
python-versions = ">=3.7"
|
||||
groups = ["main"]
|
||||
files = [
|
||||
{file = "tqdm-4.67.1-py3-none-any.whl", hash = "sha256:26445eca388f82e72884e0d580d5464cd801a3ea01e63e5601bdff9ba6a48de2"},
|
||||
{file = "tqdm-4.67.1.tar.gz", hash = "sha256:f8aef9c52c08c13a65f30ea34f4e5aac3fd1a34959879d7e59e63027286627f2"},
|
||||
]
|
||||
|
||||
[package.dependencies]
|
||||
colorama = {version = "*", markers = "platform_system == \"Windows\""}
|
||||
|
||||
[package.extras]
|
||||
dev = ["nbval", "pytest (>=6)", "pytest-asyncio (>=0.24)", "pytest-cov", "pytest-timeout"]
|
||||
discord = ["requests"]
|
||||
notebook = ["ipywidgets (>=6)"]
|
||||
slack = ["slack-sdk"]
|
||||
telegram = ["requests"]
|
||||
|
||||
[metadata]
|
||||
lock-version = "2.1"
|
||||
python-versions = ">=3.10,<4.0"
|
||||
content-hash = "bb89287de0cafdf48a8eea6717121a79981ba4c7811aca416921cceb4f89f67b"
|
||||
Reference in New Issue
Block a user