Build a Probability Calculator Project - Build a Probability Calculator Project

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

How could you say that you get the right output?
AssertionError: 0.262 != 0.272

It’s close to the expected value but different.

“Since this is based on random draws, the probability will be slightly different each time the code is run.”
This is part of the instructions.

The test considers a certain tolerance but the value you are returning is above it, I didn’t find why yet though.