# Probability calculator failed test

So I’m failing the test_prob_experiment test. I should be getting 0.272 with this code but I’m getting 0.252 instead. Can someone explain to me what those numbers mean and give me a hint on how to fix the code?

this is my code

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

class Hat:

def __init__(self, **balls):
contents = []
self.contents = contents
self.balls = balls
for key, value in self.balls.items():
for i in range(value):
contents.append(key)

def draw(self, number_draws):
drawn_balls = []

if number_draws > len(self.contents):
drawn_balls.extend(self.contents)
self.contents.clear
return drawn_balls
else:
for i in range(number_draws):
random.shuffle(self.contents)
removed_ball = self.contents.pop()
drawn_balls.append(removed_ball)
return drawn_balls

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
M = 0

for i in range(num_experiments):
expected_balls_copy = copy.deepcopy(expected_balls)
hat_copy = copy.deepcopy(hat)
drawn_items= hat_copy.draw(num_balls_drawn)

for color in drawn_items:
if color in expected_balls_copy:
expected_balls_copy[color] -= 1

if all(x <= 0 for x in expected_balls_copy.values()):
M +=1

probability = M / num_experiments
return probability
``````

and this is the output

`````` python main.py
Probability: 0.189
..F
======================================================================
FAIL: test_prob_experiment (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-probability-calculator-1/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.252 != 0.272 within 0.01 delta (0.020000000000000018 difference) : Expected experiment method to return a different probability.

----------------------------------------------------------------------
Ran 3 tests in 0.717s

FAILED (failures=1)
``````

The numbers mean instead of the expected 27.2%, your calculated propability is only 25.2%.

Testing your functions in my solution, it seems like something in your “draw()” function is wrong. If I use mine, I pass all the tests.
Though looking at it, I cannot point out what might be wrong, sorry.

Best I can say is try another way to implement the draw. Which surely isn’t satisfying ^^°

Hey thanks! I fixed it now. Something with the pop method or the shuffle method made it give the wrong probability. I still dont understand how but at least its fixed now lol

Here is the new code

``````def draw(self, number_draws):
drawn_balls = []

if number_draws > len(self.contents):
drawn_balls.extend(self.contents)
self.contents.clear
return drawn_balls
else:
for i in range(number_draws):
chosen_ball = random.choice(self.contents)
drawn_balls.append(chosen_ball)
self.contents.remove(chosen_ball)
return drawn_balls
`````````
1 Like

For the “how” part it might be useful to know that the test includes a “random-seed” which is to say, a token that is used for randomizer-operation so that they will always produce the same “random” result.

Meaning that the random-function actually produce the exact same order of items every time, allowing for a controlled test where you will get the exact same result every time, despite using randomness. In return if your function would not work exactly as expected, it would always produce the “wrong” result, even if in true randomness it might have been legit.

It’s useful in testing where if you want to eliminate the influence unpredictable randomness can have.

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