Build a Probability Calculator Project - Build a Probability Calculator Project

Tell us what’s happening:

Hi all, I’ve spent over a day trying to troubleshoot why it doesn’t pass test 3 and 4. Would love any feedback you can give me to help point me to the right direction.

My draw() seems to return the same list when the draw number exceeds the number of balls available, and the experiment() seems to match the right pattern of balls and account for it in variable M… The codes is not super pretty but it usually helps me out to right out the whole codes instead of in list comprehension. Thanks!

Your code so far

import copy
import random

class Hat:

    def __init__(self, **kwargs):
        self.contents = [ball for ball in kwargs for _ in range(kwargs[ball])]

    def draw(self, number):
        total = len(self.contents)
        draw_list = []

        if number > total:
            draw_list = self.contents
        else:
            for _ in range(number):
                random_num = random.randint(0, total-1)
                draw_list.append(self.contents.pop(random_num))
                total -= 1
            
        return draw_list
        

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    expected_list = []
    for ball in expected_balls:
        for _ in range(expected_balls[ball]):
            expected_list.append(ball)

    M = 0
    for _ in range(num_experiments):
        match = []
        new_hat = copy.deepcopy(hat)
        new_expected_list = copy.deepcopy(expected_list)
        drawn = new_hat.draw(num_balls_drawn)

        for ball in drawn:
            if ball in new_expected_list:
                match.append(ball)
                new_expected_list.remove(ball)
            
            if sorted(match) == sorted(expected_list):
                M += 1
           
    return (M/num_experiments)



hat = Hat(black=6, red=4, green=3)

probability = experiment(hat=hat,
                  expected_balls={'red':2,'green':1},
                  num_balls_drawn=5,
                  num_experiments=2000)

print(probability)


Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3 Safari/605.1.15

Challenge Information:

Build a Probability Calculator Project - Build a Probability Calculator Project

What happens with hat contents when all balls are drawn from it?

Probability values look at bit too high. What could affect that?

There will be no balls left, and all balls are drawn. And with each experiment, all the balls go back to the hat, and the drawing process repeats?

Does the draw method do it this way?

I thought so? So the draw() method will return all the balls as return values, which will be used in experiment() to compare whether it contains the .

And with each repeated experiment, you start with the same numbers of balls you initialized in the first place.

Ah, I see what you mean.

OK. I fixed that and now test 2 & 3 are good. And like you said, the probability is high now.

I try printing out the draw list and it does show that every time it draws the list is different and the expected list of balls are the same. It’s counting the matches.

OK, I just figured out. I swear every time I post here, the solution eventually pops out. I was pulling my hair out the whole day yesterday and couldn’t figure it out.

Thank you for the direction!

That’s actually very normal. To describe the issue with own words, mind needs to rethink the whole problem, what helps with finding things that were overlooked before. You might have heard about the so-called Rubber duck debugging, which is basing on the similar principle.

1 Like