Scientific Computing with Python Projects - Probability Calculator | Problem Clarification

Tell us what’s happening:
I am always failing test three. My output is 0.99 which wrong. But I’m not sure what it is i’m doing wrong. I double checked the creation of the hat class and draw methods and those are consistent with how you’d do that experiment manually. I feel like I’m either misunderstanding what constitutes an experiment, I’m assuming the hat resets after each loop in the number of experiments. Or am I misunderstanding what counts as a successful match? If for example, the requirements for a success is drawing 2 blue, 1 red, would drawing 2 blue and 2 red count since the latter is a subset of the former?

Your code so far

import random
class Hat:
    def __init__(self, **kwargs):
        self.contents = []
        for key in kwargs:
            for i in range(kwargs[key]):
    def draw(self, number):
        balls_drawn = []
        if number > len(self.contents):
            return self.contents
        for i in range(number):
            ball = random.choice(self.contents)
        return balls_drawn

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    successes = 0
    ball_dict = {}
    valid_draw = False
    for i in range(num_experiments):
        test_subject = copy.deepcopy(hat)
        balls_drawn = test_subject.draw(num_balls_drawn)
        num_match = 0
        for ball in balls_drawn:
            ball_dict[ball] = ball_dict.get(ball, 0) + 1
        for key in expected_balls:
            if expected_balls[key] <= ball_dict.get(key, 0):
                num_match += 1

        if balls_drawn == hat.contents:
            valid_draw = True
        if len(expected_balls.items()) == num_match:
            valid_draw = True
        if valid_draw:
            successes += 1
    return successes / num_experiments

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/118.0

Challenge: Scientific Computing with Python Projects - Probability Calculator

Link to the challenge:

Your description seems to be correct. The expected balls 2 blue and 1 red would be fulfilled by draw with 2 blue and 2 red balls.

Take a look at the loop with experiments, one requirement is to always start with the same number and colors of the balls in hat. What else needs to be done, so earlier experiment doesn’t affect later experiment?

I’d assume you’d have to create a new instance of the hat object for each experiment so you end up with the same hat each time. I thought I achieved this with the line:
test_subject = copy.deepcopy(hat)

but does deepcopy not create a new instance of hat that’s passed into the function? The Documentation gave that impression

deepcopy should do good job here. What is then happening with this new hat?

Should I be putting the balls back into the hat at the beginning of each experiment? I thought copying the hat into the test_subject variable was doing that

No, no :slight_smile: that’s not what I’m trying to nudge here. Look at each step what happens with that hat and balls that are drawn from it.

More immediate help might give printing which drawn balls are compared with expected balls.

I got it. So it turns out that because my comparison dictionary and boolean wasn’t reset after each loop I got extra successes which gave me the wrong probability Thank you for the helpful comments and nudging

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