Feedback regarding the Probability calculator

Hello, i’m doing the probability calculator project and am kind of stuck.

I get a much higher probability than I should. And can’t seem to find an error in my code. So maybe a fresh pair of eyes would help.

Here is my code and the link to my replit:
boilerplate-probability-calculator - Nix (beta) Repl - Replit

import copy
import random
# Consider using the modules imported above.

class Hat:
# Create a class
# Ask for a dictionary of arguments
    def __init__(self, **balls):
        """ I should Iterate over the dictionary of atributes,
        It should iterate over each key 
        and add the items to a list a number of times equal to
        its corresponding number"""
        self.contents = []
        for key in balls:
            self.contents.extend([key for value in range(balls[key])])

    def draw(self, numBalls):
        """accepts an argument indicating the number of balls to draw from the hat. 
        This method should remove balls at random from contents and return those balls as a list of strings. 
        The balls should not go back into the hat during the draw, similar to an urn experiment without replacement. 
        If the number of balls to draw exceeds the available quantity, return all the balls.
        if numBalls > len(self.contents):
            return self.contents
            randomChoices = []
            for _ in range(numBalls):
                choice = random.choice(self.contents)
            return randomChoices

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    expected_list = []
    for key in expected_balls:
        expected_list.extend([key for value in range(expected_balls[key])])
    wins = 0
    for _ in range(num_experiments):
        copy_hat = copy.deepcopy(hat)
        single_draw = copy_hat.draw(num_balls_drawn)
        if all([item in single_draw for item in expected_list]):
            wins += 1
    return wins/num_experiments

Many thanks in advance :slight_smile:

Hi @felipecabello

The issue arise from line#49 [x in list_x for y in list_y]

where it isn’t comparing the actual occurrence of the balls with specific colour, instead it is comparing the existence of the colour.


x = [1,2,2]
y = [1,1,1]
result = [True, True True] # which is incorrect as the draw isn't matching

this increased false wins hence the probability you’ve gotten is higher