# Python - Probability Calculator Challenge Issue

import random
import copy

class Hat:
def init(self, **kwargs):
self.hat = kwargs
self.contents =
for key,value in kwargs.items():
for i in range(value):
self.contents.append(key)

``````def draw(self,times):
choices = []
cp_contents = copy.deepcopy(self.contents)
for i in range(times):
try:
a = random.choice(self.contents)
choices.append(a)
self.contents.remove(a)
except:
return cp_contents
return choices
``````

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
N = 0
for i in range(num_experiments):

``````        balls_drawn_l = hat.draw(num_balls_drawn)
balls_drawn = {}
for i in balls_drawn_l:
balls_drawn[i] = balls_drawn.get(i, 0)  + 1
flag = []
for key,value in expected_balls.items():
try:
if value <= balls_drawn[key]:
flag.append(True)
else:
flag.append(False)
except:
flag.append(True)
if all(flag) == True:
N += 1
hat.contents.extend(balls_drawn_l)
return N/num_experiments``````

Tell us what’s happening. With what exactly you are having problem? What did you try already?

Hi and sorry for that… I fail the `test_prob_experiment()`

`````` expected = 0.272
self.assertAlmostEqual(actual, expected, delta = 0.01, msg = 'Expected experiment method to return a different probability.'
AssertionError: 0.469 != 0.272 within 0.01 delta (0.19699999999999995 difference) : Expected experiment method to return a different probability.)
``````

I have two questions:

1. What i am doing wrong and i have this big deviaton?
2. How can we be so sure(delta 0.01) after a 1000 experiments?

Remember that each new experiment from the `num_experiments` number of experiments should start with the same balls in hat.

To allow testing the expected result, it’s needed to make random values replicable. Test is using for that a so-called `seed` number, passed to the `random` module. Because of that when test is run, the same `seed` value is used, and calculated probability can be compared to the expected probability (for calculation of which the same `seed` was used).

Actually, i was making a big mistake in the counting of M(the number of experiments that we draw the expected balls)… I fixeed and it’s fine now… Sorry for my english and thanks for the fast reply and support This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.