Build a Probability Calculator Project - Build a Probability Calculator Project

Tell us what’s happening:

The draw is not functioning and the probability is wrong it says. I’m not sure. Thanks.

Your code so far

import copy
import random

class Hat:
    
    def __init__(self, **kwargs):
        self.contents = []
        for key, value in kwargs.items():
            for i in range(value):
                self.contents.append(key)
        print(self.contents)
        print(len(self.contents))

    def draw(self, number):
        all_removed = []
        if(number >= len(self.contents)):
            return self.contents
        else:
            for i in range(number):
                removed = self.contents.pop(int(random.random() * len(self.contents)))
                all_removed.append(removed)
            return all_removed


def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    count = 0
    for i in range(num_experiments):
        expected_copy = copy.deepcopy(expected_balls)
        hat_copy = copy.deepcopy(hat)
        colors_gotten = hat_copy.draw(num_balls_drawn)
        for color in colors_gotten:
            if(color in expected_copy):
                expected_copy[color]-=1

        if(all(x <= 0 for x in expected_copy.values())):
            count += 1
    return count / num_experiments

hat1 = Hat(black=6, red=4, green=3)
print(hat1.draw(5))
probability = experiment(
    hat1,{"red":2},11,2000)
print("Probability:",probability)

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

1 Like

In what way is the draw function not working?

It says that the draw method should behave correctly when the number of balls to remove is bigger than the number of balls in the hat.

Have you tested that behavior yourself manually? If so, what happened?

Yes, I think it’s return the right result based on what the list is. If I say like draw 14 out of the defined 13 ball hat, it’ll return the whole list and if I do the same in the experiment function it returns a probability of 1, as expected.

Have you run such a test manually?

This doesn’t check that case.

hat1 = Hat(black=6, red=4, green=3)
print(hat1.draw(13))
probability = experiment(
    hat1,{"red":2},11,2000)
print("Probability:",probability)

this returns the whole list, what I expected

Ok, and if you try to draw 14 balls?

Also, is anything left in the hat when you’re done?

import copy
import random

class Hat:
    
    def __init__(self, **kwargs):
        self.contents = []
        for key, value in kwargs.items():
            for i in range(value):
                self.contents.append(key)
        print(self.contents)
        print(len(self.contents),"\n")

    def draw(self, number):
        all_removed = []
        if(number >= len(self.contents)):
            #return self.contents
            print("Hat is now empty")
        else:
            for i in range(number):
                removed = self.contents.pop(int(random.random() * len(self.contents)))
                all_removed.append(removed)
            print("You removed:", all_removed)
            print("\nHat now contains:", self.contents)
            #return all_removed


def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    count = 0
    for i in range(num_experiments):
        expected_copy = copy.deepcopy(expected_balls)
        hat_copy = copy.deepcopy(hat)
        colors_gotten = hat_copy.draw(num_balls_drawn)
        for color in colors_gotten:
            if(color in expected_copy):
                expected_copy[color]-=1

        if(all(x <= 0 for x in expected_copy.values())):
            count += 1
    return count / num_experiments

hat1 = Hat(black=6, red=4, green=3)
print(hat1.draw(14))
'''probability = experiment(
    hat1,{"red":2},12,5)
print("Probability:",probability)'''

I commented out the return statements in my draw method and then added some print statements for a test. I also commented out the probability experiment at the end. Seems to be working… but something is still amiss.

That’s kind of cryptic, is it a riddle? Try to be more specific please.

Were you able to answer the above question?

I got it finally. Hopefully it’s okay to discuss solutions on here, but I guess because of the return self.contents under the first conditional it wasn’t “popping” anything from the original hat list, so that if the number to draw was bigger than or equal to the number in the hat, the draw method would just return the list without deleting what was in the hat. Also I guess I had to use random.randint instead of random.random and then the experiment portion worked. Thanks for getting me closer!

2 Likes

I hate how sensitive this project is to specific randomization techniques.

2 Likes

Maybe we should add a specific “Use this” to the instructions?

Or reduce the tolerance of the test?

Yeah, I’m not sure. I know they use a specific seed and are targeting a specific probability that isn’t the theoretical result so the implementation is tested.