Scientific Computing with Python Projects - Probability Calculator

Tell us what’s happening:

“”" The experiment function should return a probability.“”"

I’ve managed to complete the first 2 tests but for last one I cant seem to figure out why my “experiment” function won’t return into the right probability. Any tips or errors I should address?

Your code so far

import copy
import random

class Hat:
    def __init__(self, **kwargs):
        self.contents = []
        for key, value in kwargs.items():
            for i in range(value):
                self.contents.append(key)
            
    def draw(self, num_balls):
        if num_balls > len(self.contents):
            return self.contents
        balls = []
        for i in range(num_balls):
            random_ball = random.randint(0,(len(self.contents)-1))
            balls.append(self.contents[random_ball])
            self.contents.remove(self.contents[random_ball])
        return balls

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    count = 0
    for i in range(num_experiments):
        expected_copy = copy.deepcopy(expected_balls)
        hat_copy = copy.deepcopy(hat)
        colors_gotten = hat_copy.draw(num_balls_drawn)
        
        for color in colors_gotten:
            
            if(color in expected_copy):
                expected_copy[color] -= 1
        if(all(x <= 0 for x in expected_copy.values())):
            count += 1
        return count / num_experiments








Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4.1 Safari/605.1.15

Challenge Information:

Scientific Computing with Python Projects - Probability Calculator

where do you want to return? from inside the loop?