# Scientific Computing with Python Projects - Probability Calculator

I am failing the test_prob_experiment: I am getting 0.252 instead of 0.272 in probability
Describe your issue in detail here.

My code for experiment function is as follows:

``````def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
probability = 0.0
favourable_outcome = 0
ex_list = []
for k, v in expected_balls.items():
ex_list += v * [k]

for i in range(num_experiments):
outcome = copy.deepcopy(hat)
new_outcome = outcome.draw(num_balls_drawn)

su_nu = []

#dict1 = Counter(ex_list)
#dict2 = Counter(new_outcome)

#print(dict2)

#if dict1 == dict2:
#favourable_outcome +=1
equal = True
for i in ex_list:
if i in new_outcome:
el = new_outcome.pop(new_outcome.index(i))
su_nu.append(el)

else:
equal = False
break
if equal:
favourable_outcome+=1

#dict1 = Counter(ex_list)
#ict2 = Counter(su_nu)

#if dict1 == dict2:
# favourable_outcome +=1

probability = favourable_outcome/num_experiments

return probability
``````

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

Challenge: Scientific Computing with Python Projects - Probability Calculator

Can you post your full code? I suspect the issue is probably elsewhere.

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

class Hat:
def __init__(self, **kwargs):
self.contents = []
for k, v in kwargs.items():
self.contents += v * [k]

def draw(self, num):
new_list= []
if num > len(self.contents):
return self.contents
else:
for i in range(num):
element = self.contents.pop(int(random.random() * len(self.contents)))
new_list.append(element)

return new_list

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
probability = 0.0
favourable_outcome = 0
ex_list = []
for k, v in expected_balls.items():
ex_list += v * [k]

for i in range(num_experiments):
outcome = copy.deepcopy(hat)
new_outcome = outcome.draw(num_balls_drawn)

su_nu = []

#dict1 = Counter(ex_list)
#dict2 = Counter(new_outcome)

#print(dict2)

#if dict1 == dict2:
#favourable_outcome +=1
equal = True
for i in ex_list:
if i in new_outcome:
el = new_outcome.pop(new_outcome.index(i))
su_nu.append(el)

else:
equal = False
break
if equal:
favourable_outcome+=1

#dict1 = Counter(ex_list)
#ict2 = Counter(su_nu)

#if dict1 == dict2:
# favourable_outcome +=1

probability = favourable_outcome/num_experiments

return probability
``````

Iâ€™ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the â€śpreformatted textâ€ť tool in the editor (`</>`) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

Ok, will do next time, thanks for the info

As far as I can tell through your variable names, you have the gist correct so it has to be something small.

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