# Probability calculator results around 0.17

Hi,

I’m working through the probability calculator project for the Scientific Computing in Python course and I’m stumped on the probability values I’m getting.

I’ve seen several posts about values being around 0.17 instead of the expected 0.272 but can’t work out what I’m doing wrong here.

I’ve used print statements to check the evaluation of expected balls and balls picked and it looks like my code is picking them up OK.

I’ve looked through my ‘draw’ function but can’t see any cause for bias in there.
Any tips would be appreciated!

Your code so far

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

class Hat:
def __init__(self, **kwargs):
self.contents = []
ball_dict = {}
for key, value in kwargs.items():
i = value

while i > 0:

self.contents.append(key)

i -= 1
def draw(self, num_balls):
temp = copy.copy(self.contents)
# set up variables to use
i = num_balls
draw_list = []
# for the num of times indicated, select and random and pop (remove + return) a 'ball'
while i > 0:
sel_range = len(self.contents)
# if the number of balls exceeds the number available, continue and return what is there
if i > sel_range:
self.contents = temp
continue
else:
selector = random.randint(0, sel_range-1)
draw_list.append(self.contents[selector])
self.contents.pop(selector)
i -= 1

return draw_list

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

# turn expected_balls into a list
expected_list = []
expected_dict = dict(expected_balls)

for key, value in expected_dict.items():
i = value
while i > 0:
expected_list.append(key)
i -= 1

# running experiment for i (num of experiments)
i = num_experiments
while i > 0:
# make copy of hat for test
test_hat = copy.deepcopy(target_hat)
# finds and sorts result
result = test_hat.draw(num_balls_drawn)
check = 0

for key, value in expected_dict.items():
if result.count(key) >= value:
check += 1
if check >= len(expected_dict):
M += 1
i -= 1

return M/N

``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36 Edg/86.0.622.51`.
The runner program `main.py` does two things. First it runs a 3000 trial experiment and prints the results, which are around 0.17-0.18. Second, it runs the unit tests, one of which has an expected value of 0.272. Since your code passes, it must be computing that value correctly.