Help needed in probability calculator project

Hello guys,
I have written code for the probability calculator. As of now, one test case fails i.e.

 python main.py
Probability: 0.0006666666666666666
..F
======================================================================
FAIL: test_prob_experiment (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-probability-calculator/test_module.py", line 26, in test_prob_experiment
    self.assertAlmostEqual(actual, expected, delta = 0.01, msg = 'Expected experiment method to return a different probability.')
AssertionError: 0.002 != 0.272 within 0.01 delta (0.27 difference) : Expected experiment method to return a different probability.

----------------------------------------------------------------------
Ran 3 tests in 0.003s

FAILED (failures=1)

Here is my code

import copy
import random
# Consider using the modules imported above.

class Hat:
    def __init__(self,**kwargs):
        self.contents_dict = {}
        self.contents = []
        # self.contents_copy=copy.deepcopy(self.contents)
        for key,value in kwargs.items():
            for i in range(0,value):
                self.contents.append(key)
    
    def draw(self,number_of_balls):
        drawn_items=[]
        if number_of_balls>len(self.contents):
            return self.contents
        else:
            contents_copy=copy.deepcopy(self.contents)
            x = random.sample(range(len(self.contents)), number_of_balls)
            for i in x:
                drawn_items.append(contents_copy[i])
                self.contents.remove(contents_copy[i])
                  
            return drawn_items
        


def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    correct_results = 0
    contents=[]
    contents_copy = copy.deepcopy(hat.contents)

    for key,value in expected_balls.items():
            for i in range(0,value):
                contents.append(key)
    # print(f"contents:{contents}")
    for i in range(0, num_experiments):
        hat.contents = contents_copy
        drawn_balls = hat.draw(num_balls_drawn)
        # print(f'drawn_balls:{drawn_balls}')
        if all(items in drawn_balls for items in contents):
            correct_results+=1
    
    # print(correct_results)

    return (correct_results/num_experiments)

Tried various things but not sure where I am going wrong, can someone please help. Also, any suggestions to improve existing code will be helpful

The main problem is here:

If you add a print(hat.contents) right after this, you’ll see that it’s not the copy you expect since this will use a reference and not the copy.deepcopy() you used earlier. You need to make a new copy of the entire Hat() each time you run the experiment. If you print its contents, it should be the same before every experiment.

Also, to check your conditional testing if the draw is good, print the drawn and expected balls and you’ll see that even when the condition evaluates to true, the drawn and expected balls do not match, so you will need to change the test.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.