Tell us what’s happening:
- The experiment method should return a different probability.
my probability is really close to the one they have but it says im still failing. Anyone have any advice?
Your code so far
import copy
import random
class Hat:
def __init__(self, **kwargs):
self.contents = []
for (types, value) in kwargs.items():
self.contents += value*[types]
def draw(self,numberdrawn):
taken = []
if numberdrawn >= len(self.contents):
taken += self.contents
self.contents.clear()
return taken
else:
for i in range(numberdrawn):
x = random.choice(self.contents)
taken.append(x)
self.contents.remove(x)
return taken
def exp_draw(self,numdrawn):
exp_taken = []
if numdrawn >= len(self.contents):
exp_taken += self.contents
return exp_taken
else:
for i in range(numdrawn):
x = random.choice(self.contents)
exp_taken.append(x)
return exp_taken
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
success = 0
for i in range(num_experiments):
draws = hat.exp_draw(num_balls_drawn)
for color in list(expected_balls.keys()):
actual = draws.count(color)
expected = expected_balls[color]
if actual < expected:
break
else:
success += 1
print(success/num_experiments)
return success/num_experiments
hat = Hat(black=6, red=4, green=3)
probability = experiment(hat=hat,
expected_balls={'red':2,'green':1},
num_balls_drawn=5,
num_experiments=2000)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Challenge Information:
Build a Probability Calculator Project - Build a Probability Calculator Project