Probability Calculator's Experiment function

I have this code written along with the ‘Experiment’ function but I am not getting the correct Probability value. The experiment function is just returning 0.0 probability!

Here is my code:

import random
import copy
from collections import Counter

class Hat(object):
    def __init__(self, **colorballs): # {'red': 2, 'blue': 1}
        self.contents = []
        for k, v in colorballs.items():
            for i in range(v):

    def draw(self, nos):
        if nos > len(self.contents):
            return self.contents

            all_balls = copy.deepcopy(self.contents)
            pulled_out_balls = []
            for i in range(0, nos):
                pos = random.randint(0, len(all_balls)-1)

            return pulled_out_balls

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    group_counter = []
    for exp in range(num_experiments):

    M = group_counter.count(expected_balls)
    # print(M)
    # print(num_experiments)
    probability = M/num_experiments

    return probability

Could someone please point out what’s wrong in the ‘experiment’ function and what changes will fix this issue. TIA

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36 Edg/91.0.864.37

Challenge: Probability Calculator

Link to the challenge:

I don’t really know what your experiment is doing.
But the problem is simple: The hat.draw() function is supposed to remove balls from the had (the drawn ones). Now if you want to draw 2 times from the same hat, you need a way to get back to the original content.

Default import in this challenge includes a “copy” module, so maybe that might help.
Alternatively maybe you can find a way to get back to the original content.

Either way: you get 0.0% because you draw your hat empty in two experiments and so the following 998 or so are done with an empty hat, hence you never draw the expected balls.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.