Failing to passthe Probability Calculator due to error "The draw method should behave correctly when the number of balls to extract is bigger than the number

def draw(self, number):
    if int(number) > len(self.contents):
        return self.contents
    _drawn = []
    for _ in range(number):
        selected = random.choice(self.contents)
        _drawn.append(selected)
        self.contents.remove(selected)
    return _drawn

The hat should be emptied when a number greater than the number of balls in the hat is used. You are returning the whole content of the hat, but the hat is still full.

If this doesn’t solve your problem, please share the entire code.

I tried both to empty the contents and returning an original copy of the array, it is not working … this is the instruction “If the number of balls to draw exceeds the available quantity, return all the balls.” … If “x” total balls exists , only “y” balls need to be drawn - y > x then return x ?

class Hat:
    def __init__(self, **obj ):
        self.obj = obj
        self.contents = ("".join([ f"{key}," * value for key,value in obj.items() if value != 0 ])).split(",")[:-1]
        self.originalData = copy.deepcopy(self.contents)

    def draw(self, number):
        if number > len(self.contents):
            return self.contents
        _drawn = []
        for _ in range(number):
            select = random.choice(self.contents)
            _drawn.append(select)
            self.contents.remove(select)
        return _drawn

how have you tried to empty the contents and returning an original copy of the array?

Good Day ilenia, am using the below code:

class Hat:
    def __init__(self, **obj ):
        self.obj = obj
        self.contents = ("".join([ f"{key}," * value for key,value in obj.items() if value != 0 ])).split(",")[:-1]
        self.originalData = copy.deepcopy(self.contents)

    def draw(self, number):
        if number > len(self.contents):
            return self.contents
        _drawn = []
        for _ in range(number):
            select = random.choice(self.contents)
            _drawn.append(select)
            self.contents.remove(select)
        return _drawn

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    match = 0
    for _ in range(num_experiments):
       i = 0
       objCopy= copy.deepcopy(hat) # List will run out of data if no copy is created
       results = objCopy.draw(num_balls_drawn)
       for key,value in expected_balls.items():
           count = results.count(key)
           if count >= value:
             i+=1
       if i == len(expected_balls):
           match+=1
    return match / num_experiments

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

there is no way this is emptyint the hat, remember that a return stops the function

Hi there, I changed your code as below and it worked perfectly:

— removed solution–

1 Like

Gracious :handshake:, I have tried multiple times but my error was returning the original copy of the array when condition failed I think. Thank you

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.