Build a Probability Calculator Project - Build a Probability Calculator Project

Tell us what’s happening:

Can someone help me figure out the problem, my last test doesn’t pass?

Your code so far

import copy
import random

class Hat:
    def __init__(self, **kwargs):
        self.contents = []
        for color, count in kwargs.items():
            for i in range(count):
                self.contents.append(color)
    
    def draw(self, amount):
        if amount >= len(self.contents):
            drawn_list = []
            for i in random.sample(self.contents, k = len(self.contents)):
                drawn_list.append(i)
            self.contents.clear()
            return drawn_list   
        else:
            drawn_list = []
            for i in random.sample(self.contents, k=amount):
                drawn_list.append(i)
                self.contents.remove(i)
            return drawn_list

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    M = 0
    key = []
    value = []
    copies = []
    actual = []
    
    for i in expected_balls.keys():
        key.append(i)
    for i in expected_balls.values():
        value.append(i)
    for i in range(num_experiments):
        copies = copy.copy(hat)
        actual.append(copies.draw(num_balls_drawn))
        for n in actual[i]:
            copies.contents.append(n)
    
    for n in range(num_experiments):
        flag = 0
        for i in range(len(expected_balls)):
            if actual[n].count(key[i]) < value[i]:
                flag += 1
        if flag == 0:
            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)




















Your browser information:

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

Challenge Information:

Build a Probability Calculator Project - Build a Probability Calculator Project

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/build-a-probability-calculator-project/5e44414f903586ffb414c950.md at main · freeCodeCamp/freeCodeCamp · GitHub

this one is tricky and has been left behind, you would not find it in the new updated curriculum.

Are you sure you don’t want to try the new curriculum?

anyway, you are failing a test that uses an hat that has contents as ['blue', 'blue', 'blue', 'red', 'red', 'green', 'green', 'green', 'green', 'green', 'green']
expected_balls is {'blue': 2, 'green': 1} , num_balls_drawn is 4, and num_experiments is 1000
(the expected probability is 0.272)

you can try using a different method in draw instead of sample

I tried it and I also got my results around that number so I still didn’t realize the problem

I am sorry but it can’t be “around that number”, it must be 0.272

I also got that result included

please post your updated code

There are two ways you can format your code to make it easier to read and test:

  1. After you copy/paste your code into the editor, select it by dragging your cursor over it then click the (</>) button in the toolbar to automatically wrap your code in backticks. (You can click on the animated demo image below to enlarge it.)

  1. Manually add three backticks on a new line above your code and on a new line after your code. Note that a backtick is NOT the same as a single quote('). To find the backtick key on your keyboard, see this post.

To see changes to your post as you make them, you can click the (M+) button on the toolbar to bring up the rich text editor:

import copy
import random

class Hat:
    def __init__(self, **kwargs):
        self.contents = []
        for color, count in kwargs.items():
            for i in range(count):
                self.contents.append(color)
    
    def draw(self, amount):
        if amount >= len(self.contents):
            drawn_list = []
            for i in random.sample(self.contents, k = len(self.contents)):
                drawn_list.append(i)
            self.contents.clear()
            return drawn_list   
        else:
            drawn_list = []
            for i in random.sample(self.contents, k=amount):
                drawn_list.append(i)
                self.contents.remove(i)
            return drawn_list

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    M = 0
    key = []
    value = []
    copies = []
    actual = []
    
    for i in expected_balls.keys():
        key.append(i)
    for i in expected_balls.values():
        value.append(i)
    for i in range(num_experiments):
        copies = copy.copy(hat)
        actual.append(copies.draw(num_balls_drawn))
        for n in actual[i]:
            copies.contents.append(n)
    
    for n in range(num_experiments):
        flag = 0
        for i in range(len(expected_balls)):
            if actual[n].count(key[i]) < value[i]:
                flag += 1
        if flag == 0:
            M += 1

    return M/num_experiments
        
hat = Hat(blue=3, red=2, green=6)
probability = experiment(hat=hat,
                  expected_balls={'blue':2, 'green':1},
                  num_balls_drawn=4,
                  num_experiments=1000)
print(probability)

I am testing your code and I see a different number each time. Please maybe try a different method that is not sample. Or keep running the tests until it accidentally passes :person_shrugging:

are there any other ways I can try to fix it as I realized there’s nothing wrong ?

have you tried generating the picks of balls using a different random function?

No, I didn’t. Do you mean I need to create one more function that is used to pick the balls randomly?

No, I mean you need to change how you pick the balls in your existing function, remove sample, use a different function to generate the random balls

I already mentioned multiple times that you should change your draw to use something that is not sample to generate the extracted balls