Tell us what’s happening:
Failed:The experiment method should return a different probability.
Even though i get the right output
Your code so far
import copy
import random
class Hat:
def __init__(self, **balls):
self.contents = [x for x in balls for _ in range(balls[x])]
def draw(self, draws):
result = []
if len(self.contents) <= draws:
result = self.contents[:]
self.contents.clear()
return result
for _ in range(draws):
num = int(random.random()*(len(self.contents)))
result.append(self.contents[num])
self.contents.remove(self.contents[num])
return result
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
M = 0
N = num_experiments
for _ in range(0,N):
result_ball = {color: 0 for color in expected_balls}
box = copy.deepcopy(hat)
for ball in box.draw(num_balls_drawn):
if ball in expected_balls:
result_ball[ball] += 1
if all([result_ball.get(key) >= value for key, value in expected_balls.items()]):
M += 1
return M/N
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)
print(probability)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36
Challenge Information:
Build a Probability Calculator Project - Build a Probability Calculator Project