# Probability Calculator - Need Some Help!

Hi guys, would appreciate some help and feedback on my code. I have an issue passing test_prob_experiment in this exercise.
This was the error given: 0.256 != 0.272 within 0.01 delta (0.016000000000000014 difference) : Expected experiment method to return a different probability.

0.256 was the closest and best i could go. Feel free to look into my code written below and would appreciate any feedback or changes that i could make to it.

import random

class Hat:
def init(self, **color):
self.contents = list()
self.outofhat = list()

``````    for k, v in color.items():

while v > 0:
self.contents.append(str(k))
v -= 1

def draw(self, number):
ballsdrawn = []

if number > len(self.contents):
self.contents.extend(self.outofhat)
self.outofhat.clear()

for i in range(number):
random_draw = random.choice(self.contents)
self.contents.remove(random_draw)
ballsdrawn.append(random_draw)
self.outofhat.append(random_draw)

return ballsdrawn
``````

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
M = 0
conditions_to_meet = len(expected_balls.items())

``````for i in range(num_experiments):
conditions_met = 0
balls_drawn = hat.draw(num_balls_drawn) #balls_drawn is a list of balls drawn
print(balls_drawn)
for k,v in expected_balls.items():
if balls_drawn.count(k) >= v:
conditions_met += 1
if conditions_met == conditions_to_meet:
M += 1

probability = M/num_experiments

return probability``````

Here’s where a chunk of the problems are coming from:

The spec says to return all the balls in the hat if more are asked for than available.

The spec says that balls should not be returned to the hat. The hint about using the `copy` module is the way to go. It may be easier to make copies in the experiment as opposed to the hat.

Once you have these, the way to diagnose your experiment is to print either the failed or successful trials and search for ones that don’t belong. If your probability is low, for instance, look for failures that shouldn’t be failures.

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