# Build a Probability Calculator Project - Build a Probability Calculator Project

### Tell us what’s happening:

I’m struggling with the 3rd test on this project. I’ve looked at other people’s code on the forum but I can’t see where I’m going wrong. I am also unable to find the test data that this test uses (I’ve tried using F12 but I can’t find the data). Any help would be appreciated!

### Your code so far

``````import copy
import random

class Hat:
def __init__(self,**kwargs):
self.contents = []
for kwarg in kwargs:
for i in range(kwargs[kwarg]):
self.contents.append(kwarg)

def draw(self,no_draws):
removals = []
if no_draws > len(self.contents):
return self.contents

for i in range(no_draws):
selection = random.choice(self.contents)
self.contents.remove(selection)
removals.append(selection)
return removals

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

for i in range(num_experiments):
sacrificial = copy.deepcopy(hat)
results = sacrificial.draw(num_balls_drawn)

valid = True
for colour in expected_balls:
if results.count(colour) < expected_balls[colour]:
valid = False
break
if valid:
success += 1

prob = success/num_experiments
return prob

``````

### Your browser information:

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

### Challenge Information:

Build a Probability Calculator Project - Build a Probability Calculator Project

For some reason my code passes if I deepcopy the self.contents into a new list and then clear self.contents and return the deepcopy. Can someone explain why this works and my original code didn’t?

Don’t know what your original code was but it is only deepcopy() that creates a new instance. The other copy methods only create a new reference to the same instance. If you update the new reference, you actually update the original instance. With deepcopy() you have a new instance so you update that instance while the original instance stays intact.

Hope this helps

Think of a physical hat. If you draw all of the balls out of the hat, how many balls remain in the contents of the hat?

Thanks! This way of thinking about it makes much more sense.

1 Like