Scientific Computing with Python Projects - Probability Calculator

Tell us what’s happening:

Is this code not passing because the final result is different than what would be correct or is the testing process not forgiving to the myriad of ways this problem can be solved? I don’t see what’s wrong with my code.

Your code so far

import copy
import random

class Hat:
    def __init__(self, **ball):
        self.contents = []
        colors = []
        amounts = []
        for color, amount in ball.items():
            colors.append(color)
            amounts.append(amount)
        i = 0
        for color in colors:
            for n in range(amounts[i]):
                self.contents.append(colors[i])
            i+=1

    def draw(self, amount):
        self.drawn = []
        random.shuffle(self.contents)
        for n in range(amount):
            self.drawn.append(self.contents.pop())
        return self.drawn

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    f = 0
    for color, amount in expected_balls.items():
        for n in range(amount):
            f += 1
    print(f)
    c = 0
    for n in range(num_experiments):
        x_hat = copy.deepcopy(hat)
        drawn = x_hat.draw(num_balls_drawn)
        s = 0
        for color, amount in expected_balls.items():
            for n in range(amount):
                if color in drawn:
                    drawn.remove(color)
                    s += 1
        if s == f:
            c += 1 
    return c / num_experiments



Your browser information:

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

Challenge Information:

Scientific Computing with Python Projects - Probability Calculator

Unfortunately - yes. random.shuffle with popping last ball in contents appear to result in different answer than expected. Try utilizing random.randint or random.randrange instead.

Additionally draw method doesn’t seem to yet handle situation when number of balls to draw is higher than the balls in hat.