Build a Probability Calculator Project - Build a Probability Calculator Project

Tell us what’s happening:

I don’t understand why my code is not passing test 3. Thedraw method should behave correctly when the number of balls to extract is bigger than the number of balls in the hat

Your code so far

import copy
import random

class Hat:
    def __init__(self, **balls):
        self.contents = []
        for color, count in balls.items():
            self.contents += [color]*count
    def draw (self, number):
        if number >= len(self.contents):
            return self.contents
        drawn_balls = []
        for i in range (number):
            drawn_ball = random.choice(self.contents)
            self.contents.remove(drawn_ball)
            drawn_balls.append(drawn_ball)
        return drawn_balls

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    succ_exp = 0
    for _ in range (num_experiments):
        hat_two = copy.deepcopy(hat)
        expected_copy = expected_balls.copy()
        drawn_balls = hat_two.draw(num_balls_drawn)
        for ball in drawn_balls:
            if ball in expected_copy:
                expected_copy[ball] -= 1

        if all(count <= 0 for count in expected_copy.values()):
            succ_exp += 1

    return succ_exp / num_experiments

   

Your browser information:

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

Challenge Information:

Build a Probability Calculator Project - Build a Probability Calculator Project

If you have a hat with 4 balls in it and you draw 5 balls, then you take all 4 balls out of the hat and now the contents of the hat is empty.

Does your function work this way?