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

### Tell us what’s happening:

I can’t pass the test:
The draw method should behave correctly when the number of balls to extract is bigger than the number of balls in the hat.

why?i have tried a lot and i can’t pass it.

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

class Hat:

def __init__(self, **kwargs):
self.contents = []
for key, value in kwargs.items():
for _ in range(value):
self.contents.append(key)

def draw(self, balls: int):
if balls >= len(self.contents):
return self.contents
drawn = random.sample(self.contents, k=balls)
for ball in drawn:
self.contents.remove(ball)

return drawn
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):

expected_no_of_balls = []
for key in expected_balls:
expected_no_of_balls.append(expected_balls[key])
successes = 0

for _ in range(num_experiments):
new_hat = copy.deepcopy(hat)
balls = new_hat.draw(num_balls_drawn)

no_of_balls = []
for key in expected_balls:
no_of_balls.append(balls.count(key))

if no_of_balls >= expected_no_of_balls:
successes += 1

return successes/num_experiments
``````

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

### Challenge Information:

Build a Probability Calculator Project - Build a Probability Calculator Project

you are not decreasing the balls in `contents` in that case

``````def draw(self, balls: int):
if balls >= len(self.contents):
balls = len(self.contents)
drawn = random.sample(self.contents, k=balls)
for ball in drawn:
self.contents.remove(ball)

return drawn
``````

i’ve tried this code also, and it still doesn’t pass the test

can you explain what this code does?

well if the number of balls to be drawn is larger the number of balls in the hat,
then we change the number of balls to be drawn so that it equals the number of balls in the hat,
and then we simply draw randomly and remove a ball from the hat each time,
isn’t this what i’m supposed to to?

it asks to give back the contents array as is, the tests may not like a list in random order

i tired this in my initial code,you can check the first version i sent

self.contents there is not an empty list after the draw

yeah it is empty bro, look i wanted to draw 13 balls out of 13 balls and each time i printed the contents which was always an empty list

I’m sorry, I don’t see a `hat.contents` value being printed in your screenshot. You can also check adding this code at the end and test.

``````number_of_balls = 3 + 2 + 6
for i in range(1, number_of_balls + 3):
newhat = Hat(yellow=3, blue=2, green=6)
print("hat before drawing", newhat.contents)
drawn = newhat.draw(i)
print("hat after drawing", newhat.contents)
print("drawn", drawn)
print(
f"Length of content after draw is {max(0, number_of_balls - i)}:",
len(newhat.contents) == max(0, number_of_balls - i),
)
print(
f"Length of drawn array is {min(i, number_of_balls)}:",
len(drawn) == min(i, number_of_balls),
)
print("\n\n\n")
``````

that’s what i’m getting

It doesn’t look like you have provided your current code then

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

class Hat:

def __init__(self, **kwargs):
self.contents = []
for key, value in kwargs.items():
for _ in range(value):
self.contents.append(key)

def draw(self, balls: int):
if balls >= len(self.contents):
balls = len(self.contents)
drawn = random.sample(self.contents, k=balls)
for ball in drawn:
self.contents.remove(ball)

return drawn
def experiment(hat, expected_balls, num_balls_drawn, num_experiments):

expected_no_of_balls = []
for key in expected_balls:
expected_no_of_balls.append(expected_balls[key])
successes = 0

for _ in range(num_experiments):
new_hat = copy.deepcopy(hat)
balls = new_hat.draw(num_balls_drawn)

no_of_balls = []
for key in expected_balls:
no_of_balls.append(balls.count(key))

if no_of_balls >= expected_no_of_balls:
successes += 1

return successes/num_experiments
number_of_balls = 3 + 2 + 6
for i in range(1, number_of_balls + 3):
newhat = Hat(yellow=3, blue=2, green=6)
print("hat before drawing", newhat.contents)
drawn = newhat.draw(i)
print("hat after drawing", newhat.contents)
print("drawn", drawn)
print(
f"Length of content after draw is {max(0, number_of_balls - i)}:",
len(newhat.contents) == max(0, number_of_balls - i),
)
print(
f"Length of drawn array is {min(i, number_of_balls)}:",
len(drawn) == min(i, number_of_balls),
)
print("\n\n\n")
``````

here it is

the tests do not like a list in random order, give back the list as is

could you tell me how to do that in code?because i don’t think i understand

you need to return a list identical to the contents list

in the terminal, hat before drawing and drawn has to be the same

but i’m have to reduce the number of balls inside the hat, how am i supposed to keep the same while removing balls from it?

you can copy a list before making so that a property of hat becomes an empty list

`````` def draw(self, balls: int):
my_list = copy.copy(self.contents)
if balls >= len(self.contents):
balls = len(self.contents)
drawn = random.sample(self.contents, k=balls)
for ball in drawn:
my_list.remove(ball)

return drawn
``````

i tried this but now i’m failing test 2 and 3

and what happens now? what does this code say?